I’d like to use Google Apps Script to process events. But I can’t get past the validation of the endpoint, I keep getting “URL validation failed. Try again later.” Here’s the code.
function doPost(e) {
// Parse the JSON payload from the request
var requestData = JSON.parse(e.postData.contents);
if (requestData.event === "endpoint.url_validation") {
// Extract the plainToken from the payload
var plainToken = requestData.payload.plainToken;
// Create HMAC SHA-256 hash using the secret token as the key and plainToken as the message
var encryptedToken = Utilities.computeHmacSha256Signature(plainToken, WEBHOOK_SECRET_TOKEN);
// Convert the byte array to a hex string
encryptedToken = encryptedToken.map(function (byte) {
var hexString = (byte & 0xFF).toString(16);
return (hexString.length === 1) ? '0' + hexString : hexString;
}).join('');
// Create a response object with both tokens
var response = {
plainToken: plainToken,
encryptedToken: encryptedToken
};
// Convert the response object to JSON
var jsonResponse = JSON.stringify(response);
// Return the JSON response
return ContentService.createTextOutput(jsonResponse).setMimeType(ContentService.MimeType.JSON);
}
}
It executes without errors.
What am I doing wrong?