![](/img/trans.png)
[英]Receiving a 401 'truncated server' error when trying to add a Stripe API Key to UrlFetchApp in Google Apps Scripts
[英]How to add an API Key to a UrlFetchApp in Google Apps Scripts
解决了感谢Dimu Designs的帮助。
以下作品。
函数myFunction(){var url =“ https://api.fortnitetracker.com/v1/profile/pc/Ninja ”; var apiKey =“ xxx-xxx-xxx”;
var res = UrlFetchApp.fetch(url,{“ headers”:{“ TRN-Api-Key”:apiKey}}); var content = res.getContentText(); Logger.log(res); Logger.log(内容);
}
问题
我正在尝试使用Google表格中的Google App脚本来调用外部Fortnite API以请求播放器数据。 我停留在如何在传递请求时添加API密钥作为标头的问题。
这是我到目前为止构建的(请不要笑) !
函数myFunction(){var res = UrlFetchApp.fetch(“ https://api.fortnitetracker.com/v1/profile/PC/Ninja ?”); var content = res.getContentText(); Logger.log(res);
Logger.log(内容); }
当我尝试运行此命令时,出现以下错误:
请求失败https://api.fortnitetracker.com/v1/profile/PC/Ninja吗? 返回的代码401。服务器响应被截断:{“ message”:“在请求中未找到API密钥”}(使用MutantHttpExceptions选项检查完整响应
我已经尝试过根据许多不同的帖子以多种方式添加我的API密钥,但这是行不通的,只会使我更加困惑(在这一点上很容易做到)。
有谁知道我如何才能完成脚本以确保获得信息? :)
- -编辑 - -
首先,感谢帮助人员,这是我们目前的位置。 我现在尝试了以下方法:
var url =“ https://api.fortnitetracker.com/v1/profile/pc/Ninja ”; var apiKey =“ xxx-xxxx-xxx”;
var response = UrlFetchApp.fetch(url,{“ headers”:{“ TRN-Api-Key”:apiKey}});
这次返回的是403错误,而不是401错误。
请注意,我还尝试使用“基本”对标头进行身份验证,但这不起作用。
编辑我怀疑该API使用自定义标头。 当您注册API密钥时,将获得以下格式的字符串:
TRN-Api-Key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
我在这里猜测,但是冒号前面的文本似乎是一个自定义标头,冒号后面的字符串是API密钥。
没有适当的文档,这仍然是黑暗中的一枪,但是您可以尝试以下操作:
var url = "[FORTNITE-API-ENDPOINT]";
var apiKey = "[YOUR-API-KEY]"; // sans header and colon
var response = UrlFetchApp.fetch(
url,
{
"headers":{
"TRN-Api-Key":apiKey
}
}
);
另外,请务必查看UrlFetchApp文档以备将来参考: https : //developers.google.com/apps-script/reference/url-fetch/url-fetch-app
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.