401 Client Error: Unauthorized for url

Hello, I try to update the game and status from a channel with Python.
I create a OAuthtoken with the scope “channel_editor” and then I send a PUT request to https://api.twitch.tv/kraken/channels/<my_channel_id>

But I get this error:
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.twitch.tv/kraken/channels/<my_channel_id>

My request syntax:
requests.put(url=“https://api.twitch.tv/kraken/channels/<my_channel_id>”,
json="{‘channel’: {‘status’: ‘test title’}}", headers="{‘Accept’: ‘application/vnd.twitchtv.v5+json’, ‘Client-ID’: ‘my_client_id’, ‘Authorization’: ‘OAuth my_auth_key_with_channel_editor_scope’}"

Can anybody help me?

Is the token you’re attempting to use a User Token, or an App Token?

I dont know I, use the following URL https://id.twitch.tv/oauth2/token for a request and I use the attributs client_id, client_secret, grant_type & scope.

What grant type are you using? If it’s client_credentials then that’ll get you an App Access Token, something which wont provide access to any endpoints requiring User authorization regardless of what scopes you may try to put on it.

I see. I have a App Access…
“grant_type” : “client_credentials”,
“scope” : “channel_editor”

But how can I use a User token?

Either use the Implicit auth flow: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-implicit-code-flow which once expires you’ll have to go through the process again.

Or use the Auth Code flow: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow which will give you a refresh token too so you can generate new tokens as needed.

The reason App tokens can’t work is because they’re not associated with any user, so they have no special permissions attached to them. You need to actually send a user through the Auth flow so that they explicitly grant the app permission to access the scopes you’ve asked for.

Thank you, I will try it :grinning:

Can I use one of those methods without using a localhost or a own server?

If this is just something for your own use, what you can do is use the Auth Code flow, go to the URL on Twitch, and when you accept the scopes you’ll be redirected to localhost (which will just 404 if you’re not running a server) but you can still copy the code that is in the URL bar of your browser.

With that code you can continue the rest of the auth process where you exchange that code for a User Access Token and a Refresh Token.

I dont understand. If I load this https://id.twitch.tv/oauth2/authorize?client_id=<my_client_id>&redirect_uri=https:/twitch.tv&response_type=code&scope=channel_editor
I resive this URL: http://localhost/?error=redirect_mismatch&error_description=Parameter+redirect_uri+does+not+match+registered+URI

That’s because you’ve put the redirect URI to https:/twitch.tv, which makes no sense. The redirect URI is where Twitch will send your browser after you accept or decline connecting your Twitch account to the app.

You’re getting a redirect mismatch because that redirect URI must match what you’ve set for your app in your TwitchDev console. If you’re just doing this locally, and want to do it as I’ve mentioned, just set your redirect URL in both the URL you’re going to, and in your app settings, as http://localhost/

Okay, I changed the redirect URL to http://localhost and tried it again.
https://id.twitch.tv/oauth2/authorize?client_id=<my_client_id>&redirect_uri=http://localhost/&response_type=code&scope=channel_editor
I get to this page:
Du bist dabei, Twitch zu verlassen.

Twitch hat keine Kontrolle über den Inhalt und die Sicherheit von http://localhost.

Continue


You are about to leave Twitch.

Twitch has no control over the content and security of http: // localhost.

Continue

When I clicked “Continue” I came to a page where again the following error;

http://localhost/?error=redirect_mismatch&error_description=Parameter+redirect_uri+does+not+match+registered+URI

Go to your developer console, go to your app settings, ensure the OAuth Redirect URL is what you want it to be:

Ensure the path in the console matches EXACTLY to what you’re using the the auth URL

Oh I got it. Thank you!

Now I`ve a code in the URL, but how can I change this to an OAuthtoken?

You just follow along with the rest of the step by step guide in the docs https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow

Thanks for all :kissing_heart:

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