0
votes

I have some technical question about using apps script. A third party server is sending me parameters in POST method and the request looks like this: https://script.google.com/macros/s/AKfycbyJYVLO46T1LnQKktaMrROclCOqgawcVfZaRbm_oXfJaMIYcPj8/exec?value=$postback_params_test$ (so I need to receive $postback_params_test$ as value)

I used doPost(e) function but with no success, I thing the problem is because apps script is based on client java script and it c'ant talk with server language, am I right? or there is an option to do it anyway through apps script?

my code:

function doPost(e){
var param = e.parameter.value;
var doc = SpreadsheetApp.openById("106jpepwZZWXtpO4Id45qmJovV68q_DIqpEmTQ0khf4E");
var cell = doc.getRange('a1');
cell.setValue(param);
}

8.6

Image added: enter image description here

1
You can receive a POST request with Apps Script, although Apps Script is a javascript based language, it runs server side (on Google's servers) not client side. If you want help with this problem, you'll need to show the doPost(e) code you've tried, otherwise people can only guess at solutions. - Cameron Roberts
Thanks for reply I put the code in the main message. also web app settings are execute by :"me" and who can access to: "anyone even anonymous" , and when I send from the external sever a test postback then I get in chrome dev tool: "The remote server returned an error: (403) Forbidden.\"}"}", when I change who can access to just "anyone" then I get "responseStatusCode\:\"200"" but without any change in spreadsheet - joni m
Change it back to the settings to where you get that 200 response code. What you should do for testing purposes is return some sort of content using ContentServices. Add a try catch and return something if success or failure occurs. Now by observing your doPost code above, I believe there is a mistake causing an error. You assign SpreadsheetApp.openById() to the variable doc, and then you try to call the getRange from that variable. You should call getActive() or getActiveSheet() along with getRange(). I believe the content services would show you that there was an error. - Gerneio

1 Answers

0
votes

When deployed as a web app with settings "execute as: me" and "who has access to the app: anyone, even anonymous" your example code works.

Did you authorize the code? Before you can run it as a web app, you must run doPost() (or another function) once manually from within the script editor, so you can grant the appropriate permissions to the script.

If it's not the authorization issue, you can add a MailApp.sendemail() call to help you troubleshoot.

function doPost(e) {
    MailApp.sendEmail('YOUR-EMAIL HERE','TEST doPost()',JSON.stringify(e));

This way you'll receive an email showing the raw request coming from the other server.

Be sure to re-run the script manually after adding the MailApp line so you can authorize it to send email, and update the published version.