如何在 Google Apps 脚本中向 UrlFetchApp 添加 API 键
How to add an API Key to a UrlFetchApp in Google Apps Scripts
已解决
感谢 Dimu Designs 的帮助。
以下作品。
function 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(content);
}
问题
我正在尝试使用 Google 表格中的 Google 应用程序脚本来调用外部 Fortnite API 来请求玩家数据。我一直坚持如何在传递请求时将 API 密钥添加为 header。
这是我到目前为止构建的(请不要笑)!...
function myFunction() {
var res =
UrlFetchApp.fetch("https://api.fortnitetracker.com/v1/profile/PC/Ninja?");
var content = res.getContentText(); Logger.log(res);
Logger.log(content);
}
当我尝试 运行 时,出现以下错误:
Request failed for
https://api.fortnitetracker.com/v1/profile/PC/Ninja? returned code
401. Truncated server response: {"message":"No API key found in request"} (use muteHttpExceptions option to examine full response
我已经尝试根据许多不同的帖子以多种方式添加我的 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
}
} );
这次返回的不是 401 错误,而是 403 错误。
请注意,我还尝试使用“基本”来验证 header,但这不起作用。
编辑
我怀疑 API 使用自定义 header。当您注册一个 API 键时,您会得到一个如下形式的字符串:
TRN-Api-Key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
我在这里猜测,但冒号前面的文本似乎是自定义 header,冒号后面的字符串是 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
已解决 感谢 Dimu Designs 的帮助。
以下作品。
function 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(content);
}
问题
我正在尝试使用 Google 表格中的 Google 应用程序脚本来调用外部 Fortnite API 来请求玩家数据。我一直坚持如何在传递请求时将 API 密钥添加为 header。
这是我到目前为止构建的(请不要笑)!...
function myFunction() {
var res =
UrlFetchApp.fetch("https://api.fortnitetracker.com/v1/profile/PC/Ninja?");
var content = res.getContentText(); Logger.log(res);
Logger.log(content);
}
当我尝试 运行 时,出现以下错误:
Request failed for https://api.fortnitetracker.com/v1/profile/PC/Ninja? returned code 401. Truncated server response: {"message":"No API key found in request"} (use muteHttpExceptions option to examine full response
我已经尝试根据许多不同的帖子以多种方式添加我的 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
}
} );
这次返回的不是 401 错误,而是 403 错误。
请注意,我还尝试使用“基本”来验证 header,但这不起作用。
编辑 我怀疑 API 使用自定义 header。当您注册一个 API 键时,您会得到一个如下形式的字符串:
TRN-Api-Key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
我在这里猜测,但冒号前面的文本似乎是自定义 header,冒号后面的字符串是 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