# Python Twitch API Follow

**URL:** <https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829>\
**Category:** API\
**Created:** [April 7, 2020, 5:00am UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829 "2020-04-07T05:00:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 5:00am UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/1 "2020-04-07T05:00:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 10:17am UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/2 "2020-04-07T10:17:39Z")

</div>

First you need an oAuth loop

Either implicit or Authorization Code would suffice

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

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

> **[Reference](https://dev.twitch.tv/docs/api/reference/)**
>
> Twitch Developer tools and services to integrate Twitch into your development or create interactive experience on twitch.tv.

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.

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 2:58pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/3 "2020-04-07T14:58:33Z")

</div>

```
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](https://api.twitch.tv/kraken/users/400025983/follows/channels/44322889)”. Any help?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:00pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/4 "2020-04-07T15:00:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 3:03pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/5 "2020-04-07T15:03:10Z")

</div>

How would I fix that?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:04pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/6 "2020-04-07T15:04:21Z")

</div>

The other documentation I linked:

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

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

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 3:06pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/7 "2020-04-07T15:06:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:08pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/8 "2020-04-07T15:08:27Z")

</div>

Using this flow:

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

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

> <https://github.com/BarryCarlyon/twitch_misc/tree/main/authentication/implicit_auth>
>
> //github.com/BarryCarlyon/twitch\_misc/tree/main/authentication/implicit\_auth

Has a live demo you can try

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 3:10pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/9 "2020-04-07T15:10:23Z")

</div>

Can you write a line of code as an example?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:14pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/10 "2020-04-07T15:14:00Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 3:37pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/11 "2020-04-07T15:37:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:38pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/12 "2020-04-07T15:38:32Z")

</div>

> <https://github.com/twitchdev/issues/issues/29>
>
> \*\*Brief description\*\*
> 
> Occasionally, when you perform step 1 of oAuth (redirec…t user to Twitch to allow/deny an account link)
> 
> A JSON blob is returned in the body:
> 
> {"error":"Invalid CSRF Token"}
> 
> This is shown to a end user and the end user has no idea what to do.
> 
> \*\*How to reproduce\*\*
> 
> Keep trying to link accounts till it happens. Usually happens more often with Firefox users.
> 
> \*\*Expected behavior\*\*
> 
> Display a more useful error page, or redirect to Twitch login page, which you do get sometimes (even when logged in on Twitch)

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

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 3:44pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/13 "2020-04-07T15:44:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:45pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/14 "2020-04-07T15:45:30Z")

</div>

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/](https://barrycarlyon.github.io/twitch_misc/authentication/implicit_auth/)

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 3:46pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/15 "2020-04-07T15:46:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:47pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/16 "2020-04-07T15:47:56Z")

</div>

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](https://dev.twitch.tv/support)

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 3:50pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/17 "2020-04-07T15:50:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:51pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/18 "2020-04-07T15:51:15Z")

</div>

> [@Maxwell\_Tan](#):
>
> 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)

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

---

<div class="post-metadata">

**Author:** ![Maxwell\_Tan](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/maxwell_tan/32/8275_2.png) [@Maxwell\_Tan](https://discuss.dev.twitch.com/u/Maxwell_Tan)\
**Post date:** [April 7, 2020, 3:52pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/19 "2020-04-07T15:52:35Z")

</div>

> [@BarryCarlyon](#):
>
> 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)

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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 7, 2020, 3:55pm UTC](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829/20 "2020-04-07T15:55:31Z")

</div>

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

> **[Authentication](https://dev.twitch.tv/docs/authentication/)**
>
> Authentication

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

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

[Next page](https://discuss.dev.twitch.com/t/python-twitch-api-follow/24829.md?page=2)
