Twitch.py in discord.py

I coded a discord bot containing twitch.py with twitch helix a few months ago. Now im trying to start the Bot but get the following error:

Traceback (most recent call last):
File “H:/pythonProject/Bot/Bot.py”, line 13, in
from twitch import get_notifications
File “H:\pythonProject\Bot\twitch.py”, line 23, in
print(get_app_access_token())
File “H:\pythonProject\Bot\twitch.py”, line 20, in get_app_access_token
access_token = response.json()[“access_token”]
KeyError: ‘access_token’

What can be the problem?

Post the code.

This doesn’t seem to do or spit out anything useful other than the line numbers in your script that errored.

I would guess that the access token is dead and a new access token can’t be obtaiend as the refresh token is also dead.

I think that should be the problem:

def get_app_access_token():
params = {
“client_id”: config[“client_id”],
“client_secret”: config[“client_secret”],
“grant_type”: “client_credentials”
}

response = requests.post("https://id.twitch.tv/oauth2/token", params=params)
access_token = response.json()["access_token"]
return access_token

print(get_app_access_token())

Change

response = requests.post("https://id.twitch.tv/oauth2/token", params=params)
access_token = response.json()["access_token"]
return access_token

to

response = requests.post("https://id.twitch.tv/oauth2/token", params=params)
full_response = response.json()
print(full_response )

Which will log out the full_response that’ll provide the error message if any

Your code doesn’t check if the response code was 200 OK and/or if an access token was acturally returned or not from the API call

Yes. The client secret was the problem i think. But theres another error:

Traceback (most recent call last):
File “H:/pythonProject/Bot/Bot.py”, line 13, in
from twitch import get_notifications
File “H:\pythonProject\Bot\twitch.py”, line 80, in
users = get_users(config[“watchlist”])
File “H:\pythonProject\Bot\twitch.py”, line 41, in get_users
return {entry[“login”]: entry[“id”] for entry in response.json()[“data”]}
KeyError: ‘data’

Process finished with exit code 1

Here the code:

def get_users(login_names):
params = {
“login”: login_names
}

headers = {
    "Authorization": "Bearer {}".format(config["access_token"]),
    "Client-Id": config["client_id"]
}

response = requests.get("https://api.twitch.tv/helix/users", params=params, headers=headers)
return {entry["login"]: entry["id"] for entry in response.json()["data"]}

Suggests that response to your helix users doesn’t contain a data key

As it didn’t 200 Ok and returned an error.
So you need to fix your code to detect and handle errors more gracefully than just a “data doesn’t exist” key error.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.