提问人:Arif Ahmed 提问时间:10/5/2023 最后编辑:TheMasterArif Ahmed 更新时间:10/5/2023 访问量:76
UrlFetchApp 方法不起作用,并在从 API 检索数据时出现 405 错误?
UrlFetchApp Method does not work and gives 405 error in retrieving data from an API?
问:
我正在尝试将 Clockify 详细时间报告放入 Google 表格中。当我尝试使用 URL fetch 方法时,它给了我一个错误:
Exception: Request failed for https://reports.api.clockify.me returned code 405. Truncated server response: {"code":405, "message": "HTTP 405 Method Not Allowed"} (use muteHttpExceptions option to examine full response)
这是文档 - https://docs.clockify.me/#tag/Time-Entry-Report/operation/generateDetailedReport
我正在使用此代码:
function getTimeReport() {
var body = {"dateRangeStart":"2023-10-05T00:00:00.000Z","dateRangeEnd":"2023-05-05T23:59:59.000Z","detailedFilter":{"page":1,"pageSize":1000,"options":{"totals":"EXCLUDE"}}}
var headers = {
"headers": {
"x-Api-Key": "XXX",
"Content-type": "application/json",
"Application": "Custom",
"muteHttpExceptions": "true",
"Method": "POST",
"payload": body
}
};
var workspaceId = "XXX";
var url = `https://reports.api.clockify.me/v1/workspaces/${workspaceId}/reports/detailed`;
Logger.log(url);
var response = UrlFetchApp.fetch(url, headers);
Logger.log(response);
我仔细检查了工作区 ID 和 API 密钥都是正确的。我可以知道我哪里出了问题吗?
答:
2赞
Tanaike
10/5/2023
#1
在您的脚本中,当 的值和 的值是有效值时,如何进行以下修改?body
x-Api-Key
修改后的脚本:
function getTimeReport() {
var body = { "dateRangeStart": "2023-10-05T00:00:00.000Z", "dateRangeEnd": "2023-05-05T23:59:59.000Z", "detailedFilter": { "page": 1, "pageSize": 1000, "options": { "totals": "EXCLUDE" } } };
var options = {
"headers": { "x-Api-Key": "XXX" },
"muteHttpExceptions": "true",
"Method": "POST",
"payload": JSON.stringify(body),
"contentType": "application/json",
};
var workspaceId = "XXX";
var url = `https://reports.api.clockify.me/v1/workspaces/${workspaceId}/reports/detailed`;
Logger.log(url);
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
- 我以为这个修改后的脚本的请求与您提供的 URL 的 API 规范相同。
参考:
评论
0赞
Arif Ahmed
10/5/2023
谢谢你的回答。它给出此错误 ''' {“code”:501,“message”:“无效的日期范围。dateRangeStart 必须早于 dateRangeEnd。```
0赞
Arif Ahmed
10/5/2023
道歉。我弄错了日期。现在已修复。感谢您的帮助。欣赏它。
0赞
Tanaike
10/6/2023
@Arif 艾哈迈德:谢谢你的回复。我很高兴您的问题已得到解决。也谢谢你。
评论
POST