Check if user is streaming

So I’m trying to make that will change a variable to true if a certain streamer is live, however with the code i have now i cant even get a response back. It says {“error”:“Unauthorized”,“status”:401,“message”:“OAuth token is missing”}
and i have no idea how to fix this.

`
import requests

API_ENDPOINT = ‘https://api.twitch.tv/helix/streams?user_login=brawlhalla 42’

#example Client_ID put yours here
Client_ID = ‘kvqvvvdh0agg84q563ti0d9it7inl6’

#data to be sent to api
head = {
‘Client-ID’ : Client_ID
}
#api call here
r = requests.get(url = API_ENDPOINT, headers = head)

#data output
print(r.text)

input(‘Press ENTER to exit’)
`

All of helix requires an oAuth token

For this sort of code example you’ll use a Client Credentials Token which is for server to server requests

I’ve looked through this but i have no idea how i would implement this but if i understand it correctly my app needs to log in to twitch before it can return any data. With client id being mine and client secret being my password right?

no

you use the client ID and client secret to generate a token

Since you are python, it’s just a requests.post to get a token

using the URL documented in what I linked to

before you try to get stream status, you’ll load a previously generated token, and check if it’s valid then make the call.

Only generate a new token if needed, reuse if you can

Edit: Something like

import requests

client_id = ''
client_secret = ''
streamer_name = ''

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);

My python is rusty but I got this to work.
And it doesn’t cover token reuse just generates a new one each time

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