Accessing Live Viewer Count (Python)

I am trying to build an analytics tool using python, and I have been searching for a way to retrieve the live viewer count of a channel when it is online. Currently, I have this program, but I keep getting a 401 error saying that I need to pass in my OAuth token, which I have:

How do I pass in my oauth key and retrieve the live viewer count of the channel?

I cannot suggest a code fix, as you posted an image…

The header is “authorization” and the token needs to be preceeded by the word "Bearer " (and a space)

As shown in the example in the documentation for Get Streams

If you don’t have a user token to use then you probably want an App Access/Client Credentials token

Ok, here’s my code.
import requests


Client_ID = ‘95hkffpc2ng2zww4gttnp17y0ix14n’

oauth = ‘Bearer REMOVED’

head = {
‘Client-ID’ : Client_ID,
‘authorization’ : oauth

r = requests.get(url = API_ENDPOINT, headers = head)


I tried the changes you suggested, generating a new oauth user key. The error message now is “Client ID and OAuth token do not match”.

never mind, I got it to work, thanks for the help!

So, I was able to get an app access token yesterday with the POST command from the docs, and I was able to access the viewer count as well of a live channel. But today, with the same code, I am getting several errors.

  1. Trying to get an app access token yields a <Response[200]> instead of a json dictionary object.
    Trying to type the url with the client id and client secret in a browser results in a 404 page not found.
  2. Trying to access the viewer count with the same code, oauth token, and client id yields a <Response[401]>, which I think is the error for when an OAuth token is not valid.

Here is my current code:

Client_ID = ‘95hkffpc2ng2zww4gttnp17y0ix14n’

oauth = ‘Bearer REMOVED’

head = {

'Client_ID': Client_ID,

'Authorization': oauth


r = requests.get(url = API_ENDPOINT, headers = head)


You are GET-ing instead of POSTing

Check your code/raw response

Read the response body for the actual error message

Yeah, I was printing r instead of r.text, so that’s my fault, sorry.

I now can get the app access key, but like before, my client ID and app access token are not matching according to the error response. I have generated a new app access token that expires in 2 months, so I don’t think that’s the problem. Sorry for being a noob at this.

If the API is saying your ClientID and Token don’t match then they don’t match, you need to check your code to check you are sending what you think you are sending to Twitch

problem was a simple underscore instead of a dash in the headers. Thanks for the help!

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