google-apps-scriptdebuggingpostweb-applicationscors

Google Web App Only Logging When Accessed Via Browser


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?


Solution

  • 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.

    1. Sample script for Web Apps

    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 })
      );
    };
    

    2. Sample Google Apps Script project

    2 types of Google Apps Script projects are prepared in this test.

    1. Google Apps Script of standalone type WITHOUT linking Google Cloud Platform (GCP) Project

      • In this case, you can retrieve this standalone Google Apps Script by directly creating.
    2. Google Apps Script of standalone type WITH linking Google Cloud Platform (GCP) Project

      • In this case, you can retrieve this standalone Google Apps Script by this flow.

    3. Test for Logger.log and console.log

    To the above Web Apps of doGet and doPost, it requests the following 4 patterns.

    1. For doGet.

      $ curl -L "https://script.google.com/macros/s/###/exec"
      
    2. For doPost.

      $ curl -L -d "key=value" "https://script.google.com/macros/s/###/exec"
      
    3. For doGet. An access token is used.

      $ curl -L -H "Authorization: Bearer ###" "https://script.google.com/macros/s/###/exec"
      
    4. For doPost. An access token is used.

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

    Result and discussions

    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.

    References

    Additional information: Web App Log Visibility for Google Apps Script Ref

    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:

    Steps

    1. Create a new Google Apps Script project.
    2. Add the following script: function doGet(e) { Logger.log(JSON.stringify(e)); return HtmlService.createHtmlOutput('<b>Hello world!</b>'); }
    3. Deploy Web Apps as Execute as: Me and Who has access to the app: Anyone .
    4. Share the Google Apps Script project as Anyone with the link.
    5. Open a browser in incognito mode. Ensure you are not logged into a Google account.
    6. Access the web app and confirm that "Hello world!" is displayed.
    7. Check the log at "Executions." When the "REFRESH" button is clicked multiple times, "No logs are available for this execution" is shown.
    8. Close the browser.
    9. Open a browser in incognito mode. Log in to a Google account that is different from the owner of the Google Apps Script project.
    10. Access the web app and confirm that "Hello world!" is displayed.
    11. Check the log at "Executions." The following log entry is shown: {"contentLength":-1,"contextPath":"","parameters":{},"queryString":"","parameter":{}}
    12. Close the browser.
    13. Remove the sharing settings for the Google Apps Script project.
    14. Open a browser in incognito mode. Log in to a Google account that is different from the owner of the Google Apps Script project.
    15. Access the web app and confirm that "Hello world!" is displayed.
    16. Check the log at "Executions." The log is not shown.

    Result

    Based on these results, under the following conditions:

    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

    Note