How to use the API to get the top 20 streams

Hi.

I am trying to write a Python script to get the top 20 streams using the API. However, I could not find a guide online. I am going off of the python-twitch-client docs but so far I couldn’t find something helpful. I’ll admit, its my first time ever working with this API.

Precisely, this is what I want to accomplish: https://dev.twitch.tv/docs/api/reference#get-streams I know that the default return is 20 streams.

At the moment, this is all the code I have:

from twitch import TwitchClient
client = TwitchClient(client_id='<my client id>')

Get an access token

Then call

Call

https://api.twitch.tv/helix/streams?first=20

Pure JS example, this fetches (and estimates the view count of) the “top games”, but you can easily swap the “flow” to get the top 20 streams instead

https://barrycarlyon.github.io/twitch_misc/examples/browse_categories/

This uses implicit auth to get the needed token.

For what you describe the code would be running on a server (for caching purposes) and you’d probably use a client_credentials access token for server to server requests

For this python lib under Kraken looks like you need

from twitch import TwitchClient
client = TwitchClient(‘’)
streams = client.streams.get_live_streams()

Which would return 25 streams

But you really should be using Helix since kraken is deprecated

https://python-twitch-client.readthedocs.io/en/latest/helix.html

import twitch
client = twitch.TwitchHelix(client_id='<client_id>', client_secret='<client_secret>')
client.get_oauth()
client.get_streams()

My python is rusty.

How can I get an oauth? I couldn’t make sense of the documentation for that

The library you are using seems to do it for you

oAuth depends on the kind of token you want/need

Server to Server requests can use an App Access Token
If you have a logged in user to your app you can use a user access token generated one of two ways
User data may require a specific users user access token

So depends how you can authenticate.

I don’t think any of that is necessary in my case. I just need to retrieve the top streams so I can use my script to manually take screens…

Then you probably need

How do I include that in my code? This is what I have so far…

import twitch
client = twitch.TwitchHelix(client_id='...', oauth_token='<oauth_token>')
client.get_oauth()
client.get_streams()

Which is basically what you posted above…

I posted

import twitch
client = twitch.TwitchHelix(client_id='<client_id>', client_secret='<client_secret>')
client.get_oauth()
client.get_streams()

Sorry about my repeated dumb questions but neither code makes a difference. I am running the Python on an IDE but I don’t get an output.

Then you might need help from the libraries creators rather then this forum.

As I’m not sure what the problem could be since I don’t tend to use any Twitch libs.

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