6

Solved Thanks to Dimu Designs for helping out.

The following works.

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);

}

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.com/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.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

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 completing 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.com/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".

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 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 Aug 18 '18 at 12:47
  • There's no API documentation provided. – Ashley Henderson Aug 18 '18 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 Aug 18 '18 at 13:16
  • @tehhowch There is but its extremely poor documentation: https://fortnitetracker.com/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 Aug 18 '18 at 13:36

1 Answers1

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.com/apps-script/reference/url-fetch/url-fetch-app

TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30
  • Interesting. I tried the first answer but it still came back with a 401 error. However with this version (TRN-Api-Key) I got a 403 error. Request failed for https://api.fortnitetracker.com/v1/profile/PC/Ninja? returned code 403. Truncated server response: {"message":"Invalid authentication credentials"} (use muteHttpExceptions option to examine full response). (line 4, file "Code") I found this on GitHub, but as this is my first experience doing any coding it's like trying to learn a new language... ha! https://github.com/qlaffont/fortnite-api/blob/master/README.md – Ashley Henderson Aug 18 '18 at 14:24
  • update your original post with the updated code you used so we can inspect it – TheAddonDepot Aug 18 '18 at 14:27
  • 1
    Ahh..remove the "?" at the end of your url string and see if that works (and I'd recommend that you remove your api key as well lest someone uses it) – TheAddonDepot Aug 18 '18 at 14:35
  • And the "PC" in your url string needs to be in lowercase (just tested it and it works for me) – TheAddonDepot Aug 18 '18 at 14:38
  • haha good point about the api key. I've removed the ?, but I'm still getting the 403 error. – Ashley Henderson Aug 18 '18 at 14:38
  • Just added PC as lower case, but I'm still getting the same error message. I'll update the orginal post so we can inspect it. Maybe I've left a rogue character in there somewhere. – Ashley Henderson Aug 18 '18 at 14:40
  • I generated a new api key just to test this and it worked. So maybe your api key is invalid. Try generating a new API Key with a different email account. – TheAddonDepot Aug 18 '18 at 14:43
  • Boom it works! Thank you soooo much for your help, I really appreciate it. You've just made my day. :) :) :) – Ashley Henderson Aug 18 '18 at 15:06