最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add an API Key to a UrlFetchApp in Google Apps Scripts - Stack Overflow

programmeradmin3浏览0评论

Solved Thanks to Dimu Designs for helping out.

The following works.

function myFunction() {
     var url = ";;   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);

}

Problem

I'm trying to use Google App Scripts within Google Sheets to call the external Fortnite API to request player data. The bit I'm stuck on how to add an API Key as a header when passing the request.

This is what I've built so far (please no laughing)!...

function myFunction() {   
var res =
 UrlFetchApp.fetch("?");
var content = res.getContentText();   Logger.log(res);  
Logger.log(content); 
}

When I try to run this I get back the following error:

Request failed for ? returned code 401. Truncated server response: {"message":"No API key found in request"} (use muteHttpExceptions option to examine full response

I've tried adding my API key in a number of ways based on a number of different posts, but it doesn't work and just confuses me even more (easily done at this point).

Does anyone have any idea how I could go about pleting the script to ensure I get information back? :)

---Edit---

First of all, thanks for the help guys, here's where we are at the moment. I've now tried the following:

var url = ";; var apiKey = "xxx-xxxx-xxx";

var response = UrlFetchApp.fetch(
    url,
    {
        "headers":{
            "TRN-Api-Key":apiKey
        }
    } );

Rather than a 401 error, this time a 403 error is being returned.

Note, I've also tried to authenticate the header using "basic" but that doesn't work".

Solved Thanks to Dimu Designs for helping out.

The following works.

function myFunction() {
     var url = "https://api.fortnitetracker./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);

}

Problem

I'm trying to use Google App Scripts within Google Sheets to call the external Fortnite API to request player data. The bit I'm stuck on how to add an API Key as a header when passing the request.

This is what I've built so far (please no laughing)!...

function myFunction() {   
var res =
 UrlFetchApp.fetch("https://api.fortnitetracker./v1/profile/PC/Ninja?");
var content = res.getContentText();   Logger.log(res);  
Logger.log(content); 
}

When I try to run this I get back the following error:

Request failed for https://api.fortnitetracker./v1/profile/PC/Ninja? returned code 401. Truncated server response: {"message":"No API key found in request"} (use muteHttpExceptions option to examine full response

I've tried adding my API key in a number of ways based on a number of different posts, but it doesn't work and just confuses me even more (easily done at this point).

Does anyone have any idea how I could go about pleting the script to ensure I get information back? :)

---Edit---

First of all, thanks for the help guys, here's where we are at the moment. I've now tried the following:

var url = "https://api.fortnitetracker./v1/profile/pc/Ninja"; var apiKey = "xxx-xxxx-xxx";

var response = UrlFetchApp.fetch(
    url,
    {
        "headers":{
            "TRN-Api-Key":apiKey
        }
    } );

Rather than a 401 error, this time a 403 error is being returned.

Note, I've also tried to authenticate the header using "basic" but that doesn't work".

Share edited Apr 11, 2022 at 15:08 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Aug 18, 2018 at 10:58 Ashley HendersonAshley Henderson 631 silver badge5 bronze badges 4
  • Show the ways you've tried to include your key in the request. Also consult the API documentation - it should indicate how you are to attach your key to submit an authorized request – tehhowch Commented Aug 18, 2018 at 12:47
  • There's no API documentation provided. – Ashley Henderson Commented Aug 18, 2018 at 12:56
  • There simply must be documentation else you wouldn't have any idea how to construct your URL. Where did you get your API key? I would start there. Read about authorization. – tehhowch Commented Aug 18, 2018 at 13:16
  • @tehhowch There is but its extremely poor documentation: fortnitetracker./site-api. They don't even provide a decent sample of how to use the API key. I imagine OP will have to use the Authorization header but there are different types of token formats such as "Basic", "Bearer"...the docs don't give the developer any indication of what to use. – TheAddonDepot Commented Aug 18, 2018 at 13:36
Add a ment  | 

1 Answer 1

Reset to default 3

EDIT I suspect that the API uses a custom header. When you register for an API key you get a string in the following form:

TRN-Api-Key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

I'm guessing here, but the text preceding the colon appears to be a custom header and the string of characters after the colon is the API key.

Without proper documentation this is STILL pretty much a shot in the dark but you can try the following:

var url = "[FORTNITE-API-ENDPOINT]";
var apiKey = "[YOUR-API-KEY]"; // sans header and colon

var response = UrlFetchApp.fetch(
    url,
    {
        "headers":{
            "TRN-Api-Key":apiKey
        }
    }
);

Also, make sure to check out the UrlFetchApp documentation for future reference: https://developers.google./apps-script/reference/url-fetch/url-fetch-app

发布评论

评论列表(0)

  1. 暂无评论