Proper way to perform GET API Call with API key in header?
If you are calling an API you need to send post :
r = requests.post(url, headers=headers)
Comments
-
falconspy almost 2 years
I've never really attempted to try and write my own code that calls an API. I have some Python code that I created after discovering Python Requests library.
However, I can't get past this "authentication failed" error message.
The API key I have acquired is from https://fortnitetracker.com/site-api
The code I am using is as follows:
import requests url = 'https://api.fortnitetracker.com/v1/profile/pc/ninja' headers = {"TRN-Api-Key": "MY_KEY"} r = requests.get(url, headers=headers)
After asking for the r.status_code I get a 403 Forbidden. When I ask for r.text I get
u'{"message":"Invalid authentication credentials"}\n'
On the API page,all they say is to pass the API key in the request headers using a GET method.
I even tried passing my credentials I used to register on the site using
r = requests.get(url, headers=headers,auth=('User', 'Pass'))
Still got the same invalid authentcation credentials error.
Is what I am doing correct? What am I missing?
Thanks for your help in advance.
-
falconspy about 6 yearsu'{"message":"no API found with those values"}\n'
-
falconspy about 6 yearsGave your suggestion a go. u'{"message":"No API key found in request"}\n' - I realize they don't really provide any API documentation. That's why I am wondering if its really on my end that I am doing something wrong
-
Bono about 6 yearsIf it's easier you can also try
curl
ing the url with a header like this:curl --header "Auth: Token my_token" my_url
-
Bono about 6 yearsHm, so it looks like the header you're using is the correct one. So it really looks like you have invalid credentials. Can you try regenerating the key?
-
falconspy about 6 yearsNo I cannot. The site doesn't give me the option to generate a new key sadly.
-
Bono about 6 yearsIn that case I'd maybe suggest contacting their support department or trying to get a different key with a different account (maybe just try creating an account with 10 minute mail to get a new API key).
-
Bono about 6 yearsWhy do you think a post would be any different? Posting it done if you want to save values at their end. The OP also specifies that the API party says that a GET is required. And since he's trying to fetch data, I'd say that using a GET is correct :)