I have a weird problem with my Google web app:
function doGet(e) {
Logger.log("GET");
Logger.log(JSON.stringify(e))
return HtmlService.createHtmlOutput('<h1>Thanks for using Apps Scripts</h1>');
}
function doPost(e) {
Logger.log("POST");
Logger.log(JSON.stringify(e))
return HtmlService.createHtmlOutput('<h1>Thanks for using Apps Scripts</h1>');
}
The app is open, so everyone can access it. When I paste the URL in a (private) window, it always works as expected, doGet
is getting called and I see the log in the executions tab. However, when I access the same URL via Fetch API in the browser console or some service like Postman or Klavio, I sometimes see no logs:
Some rows are just not expandable, hence not showing anything. To be more precise, Postman and Klavio post requests never worked, Fetch API requests work (both, GET and POST) when sent from the browser console in the same tab as the Apps Script page. Could it be CORS-related? But wouldn't I see an error then, instead of the completed status? Postman requests also return status 200 and show no errors.
But it says that the function doGet
was executed (and completed), and when I send a post request, it executes doPost
as expected. How can it execute my functions without logging anything?
When you access Web Apps with a browser by logging in to your Google account, the log by Logger.log
can be recorded. But, when you access Web Apps with a script, curl, postman, Javascript on the browser, and so on, it seems that there is a rule for recording the log by Logger.log
and console.log
. In this case, I think that my report might be useful. I have never posted it on Stackoverflow. So, I would like to put it in this answer.
In order to test this, there are the following 3 steps.
This is a sample script for checking the log.
const doGet = (e) => {
Logger.log(`GET method: ${JSON.stringify(e)}`);
console.log(`GET method: ${JSON.stringify(e)}`);
return ContentService.createTextOutput(
JSON.stringify({ method: "GET", e: e })
);
};
const doPost = (e) => {
Logger.log(`POST method: ${JSON.stringify(e)}`);
console.log(`POST method: ${JSON.stringify(e)}`);
return ContentService.createTextOutput(
JSON.stringify({ method: "POST", e: e })
);
};
Execute the app as: Me
and Who has access to the app: Anyone
.2 types of Google Apps Script projects are prepared in this test.
Google Apps Script of standalone type WITHOUT linking Google Cloud Platform (GCP) Project
Google Apps Script of standalone type WITH linking Google Cloud Platform (GCP) Project
Logger.log
and console.log
To the above Web Apps of doGet
and doPost
, it requests the following 4 patterns.
For doGet
.
$ curl -L "https://script.google.com/macros/s/###/exec"
For doPost
.
$ curl -L -d "key=value" "https://script.google.com/macros/s/###/exec"
For doGet
. An access token is used.
$ curl -L -H "Authorization: Bearer ###" "https://script.google.com/macros/s/###/exec"
For doPost
. An access token is used.
$ curl -L -H "Authorization: Bearer ###" -d "key=value" "https://script.google.com/macros/s/###/exec"
The conditions that can confirm the logs are as follows.
Without access token | With access token | |
---|---|---|
Without linking GCP | Apps Script Dashboard | |
With linking GCP | Stackdriver | Apps Script Dashboard and Stackdriver |
From the above results, it was found as follows.
When using a default Google Apps Script project without linking to Google Cloud Platform (GCP), accessing the Web Apps with an access token is necessary to retrieve requested logs, even if the deployment settings are Execute the app as: Me
and Who has access to the app: Anyone
.
When using a Google Apps Script project linked to GCP, you can retrieve all user access logs for the Web Apps from Stackdriver, regardless of deployment settings like Execute the app as: Me
and Who has access to the app: Anyone
.
In order to determine whether the log of Web Apps created by Google Apps Script can be shown in the log, the following steps were performed:
function doGet(e) { Logger.log(JSON.stringify(e)); return HtmlService.createHtmlOutput('<b>Hello world!</b>'); }
Execute as: Me
and Who has access to the app: Anyone
.Anyone with the link
.{"contentLength":-1,"contextPath":"","parameters":{},"queryString":"","parameter":{}}
Based on these results, under the following conditions:
Execute as: Me
and Who has access to the app: Anyone
.The following outcomes are observed:
Logged into Google | Share Google Apps Script Project | Log Visibility |
---|---|---|
No | No | No |
No | Yes | No |
Yes | No | No |
Yes | Yes | Yes |