Stackdriver Logging API 返回响应代码 200,但响应为空 [英] Stackdriver Logging API returns response code 200, but response is empty
问题描述
我正在尝试通过 Stackdriver Logging API v2 获取 stackdriver 日志.我通过从谷歌应用程序脚本项目发出 POST 请求来做到这一点,特别是使用 UrlFetchApp.问题是,它运行成功,但日志中显示的响应为空.但是,当我使用 apirequest.io、curl 和 Google API explorer 发出相同的请求时,我得到了必要的响应.
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 {}
推荐答案
问题:
- 在
options
对象中使用了不可接受的键. - Unacceptable keys are used in
options
object. payload
是包含请求正文的唯一可接受的参数.payload
is the only acceptable parameter for including request body.
Issue:
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);
}
阅读:
- Urlfetch#参数
- 记录 api#entriesList
- 设置范围
这篇关于Stackdriver Logging API 返回响应代码 200,但响应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!