How can I get the user token and check the followers

Hello, I have a twitch bot programmed in python. I want to use the Twitch API to check if the user who sent the command follows my channel. I’ve created this function to retrieve the followers, but the request only returns the total count of followers, de data object is empty:

def FollowCheck(ctx):
Url = “https://api.twitch.tv/helix/channels/followers
AppToken = get.AppToken(idClient, ClientSecret)
idBroadcaster = get.BroadcasterId(Channel, idClient, AppToken)
idUser = get.UserId(ctx.author.name, idClient, AppToken)

headers = {
    'Authorization': 'Bearer ' + AppToken,
    'Client-Id': idClient
}

response = requests.get(url, params=params, headers=headers)
print(f"Response ({response.status_code}): {response.json()}")

json printed:
{‘total’: 44, ‘data’: , ‘pagination’: {}}

Researching a bit, I see that I need a User token to get that information. In the documentation found how to get a user token by grant flow using a link, and I’ve created this function:

def UserToken(client_id, redirect_uri, scope):
auth_url = f"https://id.twitch.tv/oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=token&scope={scope}"
webbrowser.open(auth_url)

The problem that appears when I open the page is Oops… this page cannot be accessed. Localhost refused the connection.

How Can I resolve this problem and where its placed

if this occurs after hitting [accept] on the oAuth dialog.

this indicates that your redirect URI leads to a server that is not running.

Using code flow auth expects a server to be running at the stated redirect URI in order to capture the code and then exchange the code for a user access and refresh token

Also the user that is logging in needs to be the broadcaster or a moderator for the channel you wish to read the names of…

The redirect URI that I used is “http:localhost/3000”. I justo Locked the url that appears: localhost
Apparently the URI of my code, does not mach with the application registeres, but I already cheched it and its fine

so http://localhost:3000 or http://localhost/3000

The one you use in your code needs to directly match one added to the dev console.

And in this case you need a server running on port 3000 at localhost to capture the response with

Sorry my mistake. I used http://localhost:3000 in both. The code of my bot is running in my pc. I need upload the bot to a server and run it from there or only need a server to recieve a responses?

To generate an oAuth token for a user requires a web server running on a port to capture the ?code to exchange for an access token

or something along the lines of twitch_misc/authentication/user_access_without_server at main · BarryCarlyon/twitch_misc · GitHub which uses a github pages to capture and display the ?code

And you then copy the ?code from the githubpages pages into the script running locally to complete the exchange

Alright, let me check it and see if I can correct it. Thanks!

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