Home > Mobile >  How do I use Riot Games API with an API key?
How do I use Riot Games API with an API key?

Time:02-04

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.

CodePudding user response:

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)

CodePudding user response:

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 API calls to Riot,re making the request on.

  • Related