Twitch Oauth Authorized Issue

Hello, I am having issues with my python application and making requests on behalf of the user. I have visited here

  "identified": true,
  "token": {
    "valid": true,
    "authorization": {
      "scopes": [
      "created_at": "2017-01-17T06:01:21Z",
      "updated_at": "2017-01-17T06:01:21Z"
    "user_name": "chinnbot",
    "client_id": "CLIENT ID"
  "_links": {
    "channel": "https:\/\/\/kraken\/channel",
    "chat": "https:\/\/\/kraken\/chat\/chinnbot",
    "teams": "https:\/\/\/kraken\/teams",
    "user": "https:\/\/\/kraken\/user",
    "users": "https:\/\/\/kraken\/users\/chinnbot",
    "streams": "https:\/\/\/kraken\/streams\/chinnbot",
    "ingests": "https:\/\/\/kraken\/ingests",
    "channels": "https:\/\/\/kraken\/channels\/chinnbot"

This response leads me to believe that the OAuth token is valid. Now when I try make a request on behalf of the user on my website I’m getting this response:

{u'status': 401, u'message': u'Token invalid or missing required scope', u'error': u'Unauthorized'}

The strange thing is this works when I’m testing with my main account on Twitch, but when I switch to an alternative test account this 401 response appears.

I am sending the Client ID as you can see in this pre_request:

def change_twitch_header(uri, headers, body):
    auth = headers.get('Authorization')
    if auth:
        auth = auth.replace('Bearer', 'OAuth')
        headers['Authorization'] = auth
    if app.debug:
        headers['Client-ID'] = cfg.config.twitch.dev_client_id
        headers['Client-ID'] = cfg.config.twitch.client_id
    return uri, headers, body

Here is a Gist to most of my code. This line is where I make a request on behalf of the user just to get their channel object for their identity.

Here is a link of my IDE’s debugger when my IDE hits Line 66. You can see that the me response object is returning the 401 response described above and that the twitch.authorized_response() is making the propper post and getting the access_token, refresh_token, and scope back as it should be according to the Twitch-API/Authentication Step 4

So I’m at a loss, why does it work for my main account but when I try any other account it fails. What am I doing wrong? How can I fix it?

Thank you in advance for your time.

Your code here differs slightly from your Gist code you have

auth = auth.replace('Bearer', 'OAuth')

here but over there you have

auth = auth.replace('Bearer', 'OAuth2')

Prefixing OAuth2 would cause the 401 error your seeing (I assume that just might be an oversight since it’s fixed here).

EDIT: I noticed you are calling /channel but you do not request the channel_read scope in your token which is required for that endpoint.

1 Like

Yes that was an oversight on my part. OMG @Arocide you are a genius I don’t know how I left off the channel_read scope. I was spending hours reviewing my code.

Here is another question though. Why did this work on my main account and not my test account?

It is possible that a previous version had the scope channel_read but then shouldn’t that have reset when I updated the scope?

No worries, I’ve forgotten scopes myself and wondered why I’m not authorised haha.

It is odd, is what you posted above the response from your main accounts token? Twitch doesn’t invalidate old tokens just assigns scopes for the new one. You can have many many OAuth tokens on an account for one Client-Id and each will have the scopes the token specifies but all can be used. I’d think it’s possible if you kept your old token then made a new one but forgot to update your data to the new one it would still have that old scope yeah! :smiley:

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