Python Twitch API Follow

I am generally confused about how to use the API, can anyone provide me with some code that could be used to follow another twitch channel?

First you need an oAuth loop

Either implicit or Authorization Code would suffice

Go implicit if you are building a webpage and don’t care about keeping the token.

You need a token with the relevant scope on it as documented by

Which is user_follow_edit

In python I believe the main function/library people use for making cURL style requests, is called, requests so you just need to be able to create a webpage that provides the frontfacing parts, and some code to make the oAuth hnadshake in the back and perform the actual follow.

from twitch import TwitchClient
import twitch


client = TwitchClient(client_id='CLIENT ID', oauth_token='TOKEN')
channel = client.channels.get_by_id(44322889)
client.users.follow_channel(user_id=400025983, channel_id=44322889)

This is my code above, but when I try to run it, it says “requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.twitch.tv/kraken/users/400025983/follows/channels/44322889”. Any help?

That would suggest the TOKEN doesn’t have the relevant permissions/scopes applied to it as documented on the endpoint I linked

How would I fix that?

The other documentation I linked:

You need to send the user thru an oAuth loop to obtain a token with the user_follows_edit scope applied

I understand that, but would I literally write it? I am completely new to APIs.

Using this flow:

  1. Redirect the user to Twitch
  2. If they accept they are sent back to you with a code as a query string variable
  3. Exchange the code for an access token
  4. Use the access token

Step 1 is usually delivered via a WebPage so the user can click and go to Twitch to accept/reject the Apps access to the users account.

If you go Implicit auth, that I already linked, you can do the follow stuff via pure HTML/JS using this example as a basis

Has a live demo you can try

Can you write a line of code as an example?

I linked you to an implicit auth example, already…

  1. This is an example of a outbound link for the scope needed for the follows endpoint https://id.twitch.tv/oauth2/authorize?client_id=YourClientID&redirect_uri=YourRedirect&response_type=code&scope=user_follows_edit

You may find that TwitchClient that you imported can already handle this. I’m not python native and I’m not sure how much code you want, as an example would just build the whole thing for you and you’d learn nothing

With the one above I’m getting “Invalid csrf token” as the response, any idea why?

You have a bad cookie. Relog on Twitch completely to fix.

Do you know anyone that has experience with python? I’ve tried everything you’ve told me to do, but it’s not working.

You are sending the user to that URL in a browser, and not attempting to use python to fetch that URL?

As in the example I linked here:

https://barrycarlyon.github.io/twitch_misc/authentication/implicit_auth/

All I would like it to do is to be able to follow another user…

Then you need to perform a oAuth loop to get a token you can use with the relevant scopes on it.

if you are truly stuck with the basics of an oAuth loop you can try joining the dev Discord and asking about in the #python room it’s linked from https://dev.twitch.tv/support

I was able to get a token and this is the response “{“access_token”:“TOKEN HERE”,“expires_in”:4918740,“token_type”:“bearer”}” obviously with the token being there, but how do I use that?

You already answer this one. It goes where TOKEN is in your code from earlier

I already did that, and I got the same exact response

Then it sounds like a bug/issue with TwitchClient

Assuming you successfully obtained a User Access Token with the user_follows_edit scope applied

You can check your token using the validate endpoint to assume its what you think it is

That’ll confirm the user_id and the scopes on the token

What as the URL you generated to redirect the user to?