0

I'm trying to create a Web App with a doPost() method. This POST method should receive and answer the POST request from the Webhook with a Challenge, and receive the corresponding JSON that will be placed in a Google Sheet.

Current code looks something like this:

function doPost(e) {
   // Try to answer the challenge
   return ContentService.createTextOutput(JSON.parse(e.postData.contents).challenge);

   // . . . do something else here with the JSON . . .
}

When I do the POST from Postman its returning this error:

{
"success": false,
    "message": "Invalid callback challenge key returned",
    "detail": "Expected '[Challenge_ID_Here]' but received '[Google HTML Code Here]'"
}

I set up a callback_url with https://webhook.site/ and got a successful response from it when setting it up with request.form.challenge, but I don't know how to answer with the challenge correctly with the Web App.


I successfully got the doPost() method to successfully parse the Challenge ID and print it in the Google Sheet.

I'm now trying to respond back with the ID like this:

return ContentService.createTextOutput(JSON.parse(result).parameters.challenge).setMimeType(ContentService.MimeType.JSON);

Still getting an unsuccessful response, but it is now redirecting to a script.googleusercontent.com domain, saying that the file has moved there. I enabled following redirects in Postman.

My POST request from Postman looks something like this:

curl --location --request POST '[Webhook_Subscriptions_URL]' \
--header 'Authorization: Bearer [Token_ID]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'object=myObject' \
--data-urlencode 'fields=myField1, myField2' \
--data-urlencode 'callback_url=https://script.google.com/macros/s/[ID]/exec'
Diego
  • 65
  • 1
  • 10
  • Needs debugging details/logs: https://stackoverflow.com/a/63851123/ – TheMaster Sep 24 '20 at 18:48
  • @TheMaster I can't debug from the Google Apps Script side if its not receiving anything. I did try making curl POST requests from the command line with some JSON, and got it to print into the Google Sheet. But from the Webhook I'm not receiving anything. – Diego Sep 24 '20 at 19:00
  • You should mention that in your question. Still need debugging details like: How is the webapp published? Execute as? Access?etc – TheMaster Sep 24 '20 at 19:12
  • @TheMaster its published inside GAS as Web App. Its executed with my user but anyone can access it, even anonymous. App URL is just how Google sets up every web app: https://script.google.com/macros/s/[ID_HERE]/exec – Diego Sep 24 '20 at 19:16
  • @TheMaster its just the basic HTML that comes in the Web App: ` Error
    \"Google
    – Diego Sep 24 '20 at 20:09
  • What is the error shown in that html page? – TheMaster Sep 24 '20 at 23:21
  • None. There is no error. That is returned in the POST request. – Diego Sep 24 '20 at 23:34
  • The html shows the error. Could you just [edit] in the full html? – TheMaster Sep 25 '20 at 02:33
  • @TheMaster thats the entire HTML returned in the request... – Diego Sep 26 '20 at 01:52
  • The entire html should start and end with `...`. Use ``curl`` if postman doesn't show full html. The top snippet you show clearly shows that that is a error page: `">Error<` – TheMaster Sep 26 '20 at 03:32
  • @TheMaster Just ran the command over console and got the same result. – Diego Sep 29 '20 at 04:01
  • Are you saying it ended with `....-family:monospace;m` as shown in your previous comment ? – TheMaster Sep 29 '20 at 06:54
  • You still haven't shown me the error. What exactly is the error? – TheMaster Sep 30 '20 at 01:27
  • @TheMaster The error occurs when the WebApp doesn't answer back correctly with the Challenge ID. Like the error I show in my original post. – Diego Sep 30 '20 at 01:54
  • 1
    Like I said, I need to see the full html page. It is showing a "Error" html page. This could mean a lot of things. It could be a Google login page, file not found or any other error. At least try to get the full page. Also, Another way would be to check view> stackdriver logging. See my answer [here](https://stackoverflow.com/questions/16089041) – TheMaster Sep 30 '20 at 03:29
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222283/discussion-between-diego-and-themaster). – Diego Sep 30 '20 at 03:46
  • 1
    Related: https://stackoverflow.com/a/63676056/ – TheMaster Sep 30 '20 at 17:01

0 Answers0