I have a Google Apps Script Project which I need to receive some request via GET and POST methods. Every goes fine with GET method because I can simulate it via URL in the browser; the problem is in the POST method, I can't do it via CURL it shows a Google permissions error.
Heres my code
function doPost(e) {
let response = ContentService.createTextOutput()
response.setContent(JSON.stringify({
response: e
}))
response.setMimeType(ContentService.MimeType.JSON)
return response
}
function foo()
{
var url = "https://script.google.com/macros/s/id/dev";
var headers = {"Authorization": "Bearer " ScriptApp.getOAuthToken()};
var payload = {
debug: 'data ok'
}
var params =
{
"method": "post",
"contentType": "application/json",
"headers": headers,
"payload": JSON.stringify(payload),
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(url, params).getResponseCode()
Logger.log(response);
}
I need to debug with a https://script.google.com/macros/s/id/**dev** link. The purpose is not generate a version with each test.
CodePudding user response:
In the current state, Test Deployments
are only accessible for users who have edit access to the script. This means that you need to be logged in to access this deployment, and you cannot do it through a cURL request unless you have the necessary cookies to do it.
As a workaround follow these steps:
- Open
Google Developer Tools > Network Tab
. - Go to the page where you have the Test Deployment,
/dev/
endpoint. - In the request named
dev
, click the second button and go toCopy > Copy as cURL
. This will copy the request with all the necessary cookies. - In a terminal, make the request by adding
-X POST
.
The response includes the HTML of the desired page.
Documentation
CodePudding user response:
I believe your goal is as follows.
- You want to request to your Web Apps with
https://script.google.com/macros/s/###/dev
using a curl command.
In this case, how about the following curl command?
Sample curl command:
When your payload is used, it becomes as follows.
curl -L \
-d '{"debug": "data ok"}' \
-H "Authorization: Bearer ###your access token###" \
-H "Content-Type: application/json" \
"https://script.google.com/macros/s/###/dev"
In order to access to
https://script.google.com/macros/s/###/dev
, it is required to include the access token in the request header. And as a scope, it is required to use for Drive API. For example, it'shttps://www.googleapis.com/auth/drive.readonly
.If you want to test this, you can retrieve the access token using the following sample script of Google Apps Script. You can use this access token with the above curl command.
function getAccesstoken() { console.log(ScriptApp.getOAuthToken()) // DriveApp.getFiles(); // This is used for automatically detecting the scope of `https://www.googleapis.com/auth/drive.readonly`. }
In this case,
e
ofdoPost(e)
is as follows.{"contextPath":"","parameter":{},"postData":{"contents":"{\"debug\": \"data ok\"}","length":20,"name":"postData","type":"application/json"},"contentLength":20,"queryString":"","parameters":{}}