3

enter image description here

I'm trying to test my apps script doPost function, with postman. so far I have:

function doPost(e) {

 var id = '1L_U7MhmV............................'
  SpreadsheetApp.openById(id).getSheetByName('responses').appendRow([new Date()]);
 var params = JSON.stringify(e.parameter);



 return ContentService.createTextOutput(JSON.stringify(e.parameter));
 }

I can get it working with the /exec string but when I try the /dev string I can't. I'm getting a 500 error. I'm logged into my account and have updated the version number under publish. How can I get this working?

EDIT:

Thank you. I'm trying to option 1. I created a function that logs the oath token:

function  getOAuthToken1() {    
  Logger.log('Current project has ' + ScriptApp.getOAuthToken());
}
  • I ran it and got the token. Then inserted it into

https://script.google.com/macros/s/ACCESSTOKEN/dev

but posting to this produces:

Sorry, the file you have requested does not exist.

EDIT2:

enter image description here

TheMaster
  • 45,448
  • 6
  • 62
  • 85
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 1
    When you request the endpoint of ``https://script.google.com/macros/s/###/dev``, please include the access token, even if you set "Who has access to the app: Anyone, even anonymous". Although I cannot find the document about this, I think that this is the specification. – Tanaike Jan 14 '19 at 22:52
  • I apologize for my incomplete comment. I think there are 2 patterns for your situation. From your question, I supposed that you use it as a test. 1. If you want to use a simple way, you can retrieve the access token using ``ScriptApp.getOAuthToken()`` at Google Apps Script. You can use the retrieved access token. But the expiration time is 1 hour. So when the expiration time is over, it is required to retrieve it again. But in the case that it is used as the test run, I think that this can be also used. If the scope error occurred, please add the required scopes. – Tanaike Jan 14 '19 at 23:18
  • 2. If you want to use continuously the access token, it is required to retrieve the refresh token. In order to retrieve the refresh token, for example, this thread might be helpful. https://stackoverflow.com/a/42822060/7108653 About the detail information for retrieving access token, you can see at [here](https://developers.google.com/identity/protocols/OAuth2). – Tanaike Jan 14 '19 at 23:18
  • Thank you for updating your question. I apologize for my incomplete comment again. I posted an answer for accessing to Web Apps using the endpoint of ``https://script.google.com/macros/s/###/dev``. Could you please confirm it? – Tanaike Jan 15 '19 at 01:51

1 Answers1

8
  • You want to use doPost() and access to Web Apps with the endpoint of https://script.google.com/macros/s/###/dev.

If my understanding is correct, https://script.google.com/macros/s/ACCESSTOKEN/dev cannot be used as the endpoint. Please use the original endpoint of https://script.google.com/macros/s/###/dev.

As 2 examples for the simple test, when the curl command is used, the sample commands are as follows. You can select one of them and test at your terminal. Both commands use POST method.

curl -L -H "Authorization: Bearer ### access token ###" -d "key=value" "https://script.google.com/macros/s/#####/dev"

or

curl -L -d "key=value" "https://script.google.com/macros/s/#####/dev?access_token=### access token ###"
  • In order to access to the endpoint of https://script.google.com/macros/s/###/dev of the deployed Web Apps, it is required to use the access token.
  • Replace ### access token ### to the value retrieved by ScriptApp.getOAuthToken().
  • Replace https://script.google.com/macros/s/#####/dev to your endpoint retrieved by deploying Web Apps.
  • I used -d "key=value" for the post method. If you don't want to put the values, please replace to -d "".

Note:

  • If the error related to the scopes occurs when you test above command, please add the following comment. By this, the scope of https://www.googleapis.com/auth/drive is added.
    • // DriveApp.getFiles();
    • After added it, please run the function of getOAuthToken1() again. By this, the access token including the scope can be retrieved.
Tanaike
  • 181,128
  • 11
  • 97
  • 165