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

Question:

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.

Asked By: oolio

||

Answers:

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)
Answered By: robere2

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.

Answered By: johnkhigginson
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.