我正在尝试使用 Google 应用程序脚本通过外部 API 获取数据,但我一直收到 https://api.10000ft.com 请求失败的消息返回代码 404。服务器响应被 chop :
在调试时,它告诉我响应未定义。我可能做错了什么?
这是代码:
function fetchSheet() {
var response = UrlFetchApp.fetch('https://api.10000ft.com/api/v1/users//time_entries?from=2020-01-06&to=2020-01-14&auth=token');
Logger.log(response.getContentText());
}
最佳答案
我尝试从 Postman 发送相同的请求,但收到的消息是“无效或缺少身份验证 token ”,而不是 404。 看来您需要添加 header 字段。
您应该尝试这样的操作(并检查从哪里获取访问 token ):
function fetchSheet(accessToken) {
var headers = {
headers: {
Authorization: 'Bearer ' + accessToken
}
}
var response = UrlFetchApp.fetch('https://api.10000ft.com/api/v1/users/time_entries?from=2020-01-06&to=2020-01-14&auth=token', headers);
Logger.log(response.getContentText());
}
此外,该网址似乎不符合 REST 的结构:.../users/HERE_SHOULD_BE_A_USER_ID/time_entries?...
我猜你最初使用双斜杠的错误是你错过了这个路径参数。
关于Javascript API GET 请求失败,代码为 404。服务器响应被 chop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59747186/