Line 17 of my example, will print out the “keys”, one of the items in the object will be expires_in that tells you how long the key is good for. Generally speaking App Access Tokens last around 60 days.
You can also use the Validation endpoint, Authentication | Twitch Developers to check a tokens validity and current time left
I modifed my github example
import requests
client_id = ''
client_secret = ''
streamer_name = 'bikestreaming'
body = {
'client_id': client_id,
'client_secret': client_secret,
"grant_type": 'client_credentials'
}
r = requests.post('https://id.twitch.tv/oauth2/token', body)
#data output
keys = r.json();
print(keys)
headers = {
'Client-ID': client_id,
'Authorization': 'Bearer ' + keys['access_token']
}
print(headers)
stream = requests.get('https://api.twitch.tv/helix/streams?user_login=' + streamer_name, headers=headers)
stream_data = stream.json();
print(stream_data);
if len(stream_data['data']) == 1:
print('live')
else:
print('not live');
And this works (well aside from the target streamer now being offline)
My len() line is wrong in what I modified earlier
This code example is not a production ready example, you shouldn’t generate a new token every time