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

依然范特西╮ 提交于 2020-06-29 06:56:57

问题


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 {}


回答1:


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:

  • Urlfetch#params
  • Logging api#entriesList
  • Setting scopes


来源:https://stackoverflow.com/questions/54575894/stackdriver-logging-api-returns-response-code-200-but-response-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!