oolio
oolio

Reputation: 25

How do I use Riot Games API with an API key?

I was trying to connect to the Riot Games API with the Python requests module, and it keeps giving me a 401 error. I added an API key, but it still says unauthorized. If anyone knows what is wrong with the code it would be appreciated.

I have tried tinkering and all I have this code:

import os
import requests

API_KEY = os.getenv("riot-key")

URL = "https://americas.api.riotgames.com/riot"

headers = {
    "Authorization": "Bearer " + API_KEY
}

response = requests.get(URL, headers=headers)

if response.status_code == 200:
    print(response.json())
else:
    print("Request failed with status code:", response.status_code)

All I really have concluded is that the API key itself is not the issue, it is the request call.

Upvotes: 2

Views: 2071

Answers (2)

johnkhigginson
johnkhigginson

Reputation: 156

Use your api key as a parameter rather than a header.

https://americas.api.riotgames.com/riot/?api_key=YOUR-API-KEY

Here is some help I found: https://apipheny.io/riot-games-api/#:~:text=All%20API%20calls%20to%20Riot,re%20making%20the%20request%20on.

Upvotes: 1

robere2
robere2

Reputation: 1716

It looks like Riot Games API uses the header X-Riot-Token to pass the authentication token, not Authorization, for some reason.

import os
import requests

API_KEY = os.getenv("riot-key")

URL = "https://americas.api.riotgames.com/riot"

headers = {
    "X-Riot-Token": API_KEY
}

response = requests.get(URL, headers=headers)

if response.status_code == 200:
    print(response.json())
else:
    print("Request failed with status code:", response.status_code)

You can also pass the API key as a query string parameter, however this can be slightly less secure in some scenarios.

import os
import requests

API_KEY = os.getenv("riot-key")

URL = "https://americas.api.riotgames.com/riot?api_key=" + API_KEY

response = requests.get(URL)

if response.status_code == 200:
    print(response.json())
else:
    print("Request failed with status code:", response.status_code)

Upvotes: 1

Related Questions