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

5
  • 1
    Your use of Client-ID and OAuth headers does not match what's shown in dev.twitch.tv/docs/…. Commented Aug 16, 2020 at 9:25
  • How can i generate a oauth key Commented Aug 16, 2020 at 10:15
  • Getting a token is also shown on that page. Commented Aug 16, 2020 at 10:16
  • i ve tried it but i dont get it . I am stuck on this like days the old api was a lot better Commented Aug 16, 2020 at 10:20
  • If there's a specific problem with your code, give a minimal reproducible example. Otherwise you'll have to contact their support. Commented Aug 16, 2020 at 10:23

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

2 Comments

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)
Then you need to create a chat bot that connect to the IRC server. There is no kraken (or helix) involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.