Search code examples
google-apps-scriptstackdrivergoogle-cloud-stackdriver

Stackdriver Logging API returns response code 200, but response is empty


I'm trying to fetch stackdriver logs via Stackdriver Logging API v2. I do this by making a POST request from google apps script project, in particular using UrlFetchApp. The thing is, it runs successfully, but the response shown in log is empty. However, when I made the same request using apirequest.io, curl and Google API explorer, I got the necessary response.

I searched extensively, but to no avail. Tried experimenting with header, url, but nothing.

function exportLogs () {
    var options = {
    "method" : "post",
    "headers": {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()},
    "resourceNames": [
        "projects/MyProject"
    ],
    "pageSize": 1,
    }
    var response = UrlFetchApp.fetch('https://logging.googleapis.com/v2/entries:list?key=MyApiKey', options)
    Logger.log(response)
}

What I want to get is some logs, but I'm only getting {}


Solution

  • Issue:

    • Unacceptable keys are used in options object.

    Solution:

    • payload is the only acceptable parameter for including request body.

    Code:

    function exportLogs() {
      var options = {
        method: "post",
        headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() }, //Include  https://www.googleapis.com/auth/cloud-platform in scopes
        payload: JSON.stringify({
          resourceNames: ['projects/[PROJECT_ID]'],
          pageSize: 1,
        }),
      };
      var response = UrlFetchApp.fetch(
        'https://logging.googleapis.com/v2/entries:list?key=MyApiKey',
        options
      );
      Logger.log(response);
    }
    

    To Read: