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

**URL:** <https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162>\
**Category:** API\
**Created:** [November 19, 2020, 4:26pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162 "2020-11-19T16:26:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![GianniKoch](https://avatars.discourse-cdn.com/v4/letter/g/8edcca/32.png) [@GianniKoch](https://staging-discuss.dev.twitch.com/u/GianniKoch)\
**Post date:** [November 19, 2020, 4:26pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/1 "2020-11-19T16:26:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [November 19, 2020, 4:31pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/2 "2020-11-19T16:31:02Z")

</div>

Step 1) Setup a user oAuth flow [https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-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](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](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](https://github.com/BarryCarlyon/twitch_misc/tree/master/authentication/user_access_generator/nodejs)

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

---

<div class="post-metadata">

**Author:** ![GianniKoch](https://avatars.discourse-cdn.com/v4/letter/g/8edcca/32.png) [@GianniKoch](https://staging-discuss.dev.twitch.com/u/GianniKoch)\
**Post date:** [November 19, 2020, 4:32pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/3 "2020-11-19T16:32:37Z")

</div>

> [@BarryCarlyon](#):
>
> That should get you going but where are y

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

---

<div class="post-metadata">

**Author:** ![GianniKoch](https://avatars.discourse-cdn.com/v4/letter/g/8edcca/32.png) [@GianniKoch](https://staging-discuss.dev.twitch.com/u/GianniKoch)\
**Post date:** [November 19, 2020, 5:05pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/4 "2020-11-19T17:05:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GianniKoch](https://avatars.discourse-cdn.com/v4/letter/g/8edcca/32.png) [@GianniKoch](https://staging-discuss.dev.twitch.com/u/GianniKoch)\
**Post date:** [November 19, 2020, 6:24pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/5 "2020-11-19T18:24:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [November 19, 2020, 6:31pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/6 "2020-11-19T18:31:55Z")

</div>

> [@GianniKoch](#):
>
> First question, for how long will this code work?

The call to generate a token returns the _expires in_ time

You can also use the validate endpoint

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

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

> [@GianniKoch](#):
>
> What do you recommend doing here? Should I check when a response is token expired ?

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

---

<div class="post-metadata">

**Author:** ![GianniKoch](https://avatars.discourse-cdn.com/v4/letter/g/8edcca/32.png) [@GianniKoch](https://staging-discuss.dev.twitch.com/u/GianniKoch)\
**Post date:** [November 19, 2020, 6:43pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/7 "2020-11-19T18:43:46Z")

</div>

> [@BarryCarlyon](#):
>
> The call to generate a token returns the _expires in_ time

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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [November 19, 2020, 6:45pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/8 "2020-11-19T18:45:20Z")

</div>

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

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

Step 3:

```auto
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>

```

---

<div class="post-metadata">

**Author:** ![GianniKoch](https://avatars.discourse-cdn.com/v4/letter/g/8edcca/32.png) [@GianniKoch](https://staging-discuss.dev.twitch.com/u/GianniKoch)\
**Post date:** [November 19, 2020, 6:46pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/9 "2020-11-19T18:46:14Z")

</div>

> [@BarryCarlyon](#):
>
> gives you a JSON blob with the actual access token AND the expires time for that tok

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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [November 19, 2020, 6:47pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/10 "2020-11-19T18:47:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://d2spem0quai2ol.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://staging-discuss.dev.twitch.com/u/system)\
**Post date:** [December 19, 2020, 6:47pm UTC](https://staging-discuss.dev.twitch.com/t/get-my-own-subscribers-from-a-request-where-to-start/29162/11 "2020-12-19T18:47:20Z")

</div>

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