Get my own subscribers from a request? Where to start?

Hello,

I have a community discord where I want to show my number of subscribers in real time without having to update it myself. I use node.js to edit the info in the server via discord.js. Now I have the problem that I can’t see my subscribers without an oauth token with scope channel:read:subscribers. I’ve been with this problem for over 2 weeks now and I try something different every day but I get stuck. Can someone help me on my way?

EDIT: I forgot to say, I’m trying this with a request from my server (node.js file running)?

Kind regards,
Gianni

Step 1) Setup a user oAuth flow https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow
Step 2) Get a token for your own account with channel:read:subscriptions applied to it
Step 3) Call the subscribers endpoint periodically to total up your subscribers https://dev.twitch.tv/docs/api/reference#get-broadcaster-subscriptions
Step 4) When your token expires, use the refresh token to get a new token https://dev.twitch.tv/docs/authentication#refreshing-access-tokens

This NodeJS example demonstrates a server capable of obtaining a User Access Token https://github.com/BarryCarlyon/twitch_misc/tree/master/authentication/user_access_generator/nodejs

That should get you going but where are you getting stuck?

1 Like

Thank you for the very quick response, I’m following the steps and I’m gonna check out the github page.

I just saw your question on the bottom, so yea step 1 basicly… doing it on a server. The github page kinda helped but I’m not 100% sure. Testing it rn.

hey @BarryCarlyon, I got myself a “code” to use in the Access Token. First question, for how long will this code work? And second, in step 4 the docs says " However, you should build your applications in such a way that they are resilient to token authentication failures. In other words, an application capable of refreshing tokens should not need to know how long a token will live. Rather, it should be prepared to deal with the token becoming invalid at any time. " What do you recommend doing here? Should I check when a response is token expired ? or just set a timeout that runs when the time should be over for a new AT?

The call to generate a token returns the expires in time

You can also use the validate endpoint

To validate a token and get the remaining time on a token.

Depends what I’m using the token for.

For subscribers, like your use case, I always attempt to refresh the token before I fetch all subscribers, because I either fetch once per day, (so the token will always be dead), or my the time schedule I use I’ll do a validate first to check the token and refresh as needed to fit my time schedule

1 Like

I meant this code : uri?code= this code &scope=channel%3Aread%3Asubscriptions

Thats step 2 of oAuth

They come back to your site with a code

You then make a HTTP Post to exchange the code for a token.

That gives you a JSON blob with the actual access token AND the expires time for that token

Step 3:

POST https://id.twitch.tv/oauth2/token
    ?client_id=<your client ID>
    &client_secret=<your client secret>
    &code=<authorization code received above>
    &grant_type=authorization_code
    &redirect_uri=<your registered redirect URI>

so aslong I use my refresh token I shouldn’t have to redo the get a code via uri step?

Correct.

When you refresh you either get a new token (and a possible new refresh token)

or you get an error and you have to redo the auth over again. Which is rare/happens due to other criteria

1 Like