[Python] Function to get Twitch Channel ID

Here’s my function:

def get_channel_id():
url = "https://api.twitch.tv/kraken/channel/?scope=channel_read/"
channel_id = urllib.request.Request(url)

channel_id.add_header("Client-ID", CLIENT_ID)

#defined w/o "oath:" at the beginning
channel_id.add_header("Authorization", "OAuth " + CHANNEL_OAUTH) 

response = urllib.request.urlopen(channel_id)
tmpJSON = json.loads(response.read())
return str(tmpJSON['_id'])

Returns me urllib.error.HTTPError: HTTP Error 401: Unauthorized when getting response.

A 401 means your token doesn’t have the correct scope attached to it. You can check to make sure your token has the correct scope by passing it to this URL: https://api.twitch.tv/kraken?oauth_token=

Also, just as an aside, the ?scope=channel_read on your URL doesn’t do anything for you. :slight_smile:

Thanks for your answer.
So, I don’t need to add scope to my url? I added that before because I was getting HTTP Error 404: not found.
I just modified my url so it would look like:

url = "https://api.twitch.tv/kraken?oauth_token="+CHANNEL_OAUTH+""

But I am still getting Error 404. I already tried to go to the url and it works, telling me that the scope is “chat_login”, which I suppose is fine.

I really don’t understand what I’m doing wrong.

Scope is defined when you’re generating an OAuth token as described in the authentication guide. I would highly recommend reading that and spend some time learning OAuth if you’re unfamiliar with it.

As stated in the documentation, https://api.twitch.tv/kraken/channel needs the channel_read scope. Your token, as you saw, only has the chat_login scope. You’ll need to generate a new token with the channel_read scope and pass that into your original code.

I will read again all API documentation, because apparently something is not clear to me.

I need to create a new OAuth that allows me to define multiple scopes, from chat_login to channel_read or even user_read, am I right?

Your OAuth scopes will depend on which API endpoints or capabilities you’re using. For example, Get User uses user_read scope, Get Channel uses channel_read, chat uses chat_login scope. Each endpoint documents whether it requires a specific OAuth scope or not.

When you’re generating an OAuth token for a user, you should request only the smallest amount of scopes that you need to operate. A single token can have multiple scopes to it, but it’s up to you to request them when the user logs in.

Ok. Now I think I’m getting it. I created a new token with scope “channel_read”, I checked the scope in https://api.twitch.tv/kraken?oauth_token=mytoken and I saw scope is correctly defined.
But still, I am getting the Unauthorized error.

Can you please post the code you’re using with the token redacted?

Obviously:

def get_channel_id():
url = "https://api.twitch.tv/kraken/channel/"
channel_id = urllib.request.Request(url)

channel_id.add_header("Client-ID", CLIENT_ID)
channel_id.add_header("Authorization", "OAuth " + "blablabla")

response = urllib.request.urlopen(channel_id)
tmpJSON = json.loads(response.read())
return str(tmpJSON['_id'])

It’s nearely the same as the one I use to get user id.

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