2

I am attempting to get user statistics from the Fortnite tracker api.

I have an api key and am using the correct url as indicated in the documentation

Template url:

https://api.fortnitetracker.com/v1/profile/{platform}/{epic-nickname}

Desired url:

https://api.fortnitetracker.com/v1/profile/pc/xantium0

If I use this link in browser I get {"message":"No API key found in request"} (as I have not passed the API key) so the link should be correct. Also if I do not pass the api key with urllib then I still get a 403 error.

I have checked out how to pass a header in a request: How do I set headers using python's urllib?

and so far have this code:

import urllib.request as ur

request = ur.Request('https://api.fortnitetracker.com/v1/profile/pc/xantium0', headers={'TRN-Api-Key' : 'xxx'})

response = ur.urlopen(request)

print(response.read())

When run I get this error:

urllib.error.HTTPError: HTTP Error 403: Forbidden

403 checks out as:

HTTP 403 is a standard HTTP status code communicated to clients by an HTTP server to indicate that the server understood the request, but will not fulfill it. There are a number of sub-status error codes that provide a more specific reason for responding with the 403 status code.

https://en.wikipedia.org/wiki/HTTP_403

The response is the same if I don't pass the api key in the header.

I can only think of three reasons this code is not working:

  • I have passed the wrong header name (i.e. it's not TRN-Api-Key)
  • My code is incorrect and I am not actually passing a header to the server
  • I have been banned

My problem is that I think my code is correct:

From the documentation:

urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

I have passed the url and I have passed the headers (wihout confusing with the data arguement). The api documentation also mentions it should be passed in the headers.

I am also quite sure I need to use the TRN-Api-Key as it is shown in the api documentation:

TRN-Api-Key: xxx

Also in this question (using Ruby):

header = {
    key: "TRN-Api-Key: Somelong-api-key-here"
}

Or I have been banned (this is possible although I got the key 15 minutes ago) is there a way to check? Would this error be returned?

What is preventing me from getting the user statistics?

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • Have you looked at using [swagger](https://swagger.io/) before? – Daniel Loudon Sep 03 '18 at 22:19
  • Hey @Simon , I was also having issues connecting to the API , however even without a header I was receiving a 401 error, so probably banned? How did you know to pass "TRN-Api-Key" as the header name? As soon as I did that I now am receiving a 200. I was trying names like API_KEY and other things. Couldn't find it anywhere on the website – mitchell Feb 23 '21 at 00:56
  • @mitchell Unfortunately not. and I gave up – Xantium Feb 23 '21 at 13:17

1 Answers1

1

Try using requests, a pythonic, fast and widely used module.

import requests


url = 'https://api.fortnitetracker.com/v1/profile/pc/xantium0'
headers = {
    'TRN-Api-Key' : 'xxx'
}

response = requests(url, headers=headers)

print('Requests was successful:', response.ok)
print(response.text)

If it doesn't work you can visit the url with your browser, then check the requests: in Firefox press Cntrl+Shift+E, in Chrome Cntrl+E (or Inspect with Cntrl+Shift+I and then go to Network). Press on "https://api.fortnitetracker.com/v1/profile/pc/xantium0" and change the headers. On Firefox there's the button Modify and resend. Check the response and eventually, try to change the header api key name.

Hope this helps, let me know.

Federico Rubbi
  • 714
  • 3
  • 16
  • Ok I changed your code to remove the error: `response = requests.get(url, headers=headers)`. When it runs I get this `Requests was successful: False {"message":"Invalid authentication credentials"}`. This looks like the key is wrong but it matches exactly. – Xantium Sep 13 '18 at 21:40