0
import requests

if __name__ == '__main__':
    API = 'https://api.twitch.tv/helix/streams?user_login=rediban'
    Client_ID = "myid"
    OAuth = "mytoken"

    head = {
        'Client-ID': Client_ID,
        'OAuth': OAuth,
    }
    rq = requests.get(url=API, headers=head)
    print(rq.text)

I would like to have the currently live viewer in a stream. When i start the script it says {"error":"Unauthorized","status":401,"message":"OAuth token is missing"}. Hope you can help me :)

Noah Schmidt
  • 26
  • 1
  • 7

1 Answers1

1

Here is an example, you'll need to provide your own ClientID and Secret and the stream_name you want to lookup. You can sign up for CID at https://dev.twitch.tv/console/apps

This example will generate a Client Credentials token each time which is bad practice. You should (normally) generate and store then resuse a token till it expires.

It generates a token, then calls the streams API to lookup the live status


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

if len(stream_data['data']) == 1:
    print(streamer_name + ' is live: ' + stream_data['data'][0]['title'] + ' playing ' + stream_data['data'][0]['game_name']);
else:
    print(streamer_name + ' is not live');
Barry Carlyon
  • 1,039
  • 9
  • 13
  • Hi, how do I get from here to sending messages to my channel? The only tutorials I have found use kraken! (I also want it to be able to work while im not streaming, in case anybody wants to use the command at any time) –  Jul 23 '21 at 01:23
  • Then you need to create a chat bot that connect to the IRC server. There is no kraken (or helix) involved. – Barry Carlyon Jul 24 '21 at 11:22