Proper way to perform GET API Call with API key in header?

11,434

If you are calling an API you need to send post :

r = requests.post(url, headers=headers)

Share:
11,434
falconspy
Author by

falconspy

Just some IT guy that works at a fortune 100 company

Updated on June 09, 2022

Comments

  • falconspy
    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
    falconspy about 6 years
    u'{"message":"no API found with those values"}\n'
  • falconspy
    falconspy about 6 years
    Gave 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
    Bono about 6 years
    If it's easier you can also try curling the url with a header like this: curl --header "Auth: Token my_token" my_url
  • Bono
    Bono about 6 years
    Hm, 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
    falconspy about 6 years
    No I cannot. The site doesn't give me the option to generate a new key sadly.
  • Bono
    Bono about 6 years
    In 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
    Bono about 6 years
    Why 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 :)