For Helix Subscriber Count

How to get subscriber and followers count for my website ? i tried but not working. Can you example Any code used?

Old Code (For followers, Kraken it still works but it won’t work soon. :frowning: )

$twitchlink = "https://api.twitch.tv/kraken/channels/channel id ";

$twitchcurl = curl_init($twitchlink);
curl_setopt($twitchcurl, CURLOPT_URL, $twitchlink);
curl_setopt($twitchcurl, CURLOPT_RETURNTRANSFER, true);

$twitchauth = array(
“Accept: application/vnd.twitchtv.v5+json”,
"Client-ID: (my client id) ",
);
curl_setopt($twitchcurl, CURLOPT_HTTPHEADER, $twitchauth);
//for debug only!
curl_setopt($twitchcurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($twitchcurl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($twitchcurl);
curl_close($twitchcurl);

$twitchveri = json_decode($resp, true);
$followers = $twitchveri[‘followers’];

echo $followers;

You need to mgirate to Helix. Refer to the migration guide, it lists how a given kraken endpoint maps to a helix one:

https://dev.twitch.tv/docs/api/migration

for the subscribers count you will need permission from the broadcaster you wish to read subs from in order to read their subs.

For Subscribers

Get a user authentication token:

And loading a subscriber count

For followers: any sort of token will do

So since you want subscriber count you’ll use the broadcasters token to get both follower and sub count, so you won’t have to juggle two tokens.

So your code becomes (untested off the top of my head)

Please note the added error checking of JSON and HTTP Status Codes.

<?php

$twitchlink = "https://api.twitch.tv/helix/users/follows?to_id=CHANNELID&first=1";

$twitchcurl = curl_init($twitchlink);
curl_setopt($twitchcurl, CURLOPT_URL, $twitchlink);
curl_setopt($twitchcurl, CURLOPT_RETURNTRANSFER, true);

$twitchauth = array(
"Client-ID: (my client id) ",
'Authorization: Bearer TOKENHERE'
);
curl_setopt($twitchcurl, CURLOPT_HTTPHEADER, $twitchauth);

$resp = curl_exec($twitchcurl);
$i = curl_getinfo($twitchcurl);
curl_close($twitchcurl);

if ($i['http_code'] == 200) {
    $twitchveri = json_decode($resp, true);
    if (json_last_error() == JSON_ERROR_NONE) {
        $followers = $twitchveri->total;
        echo $followers;
    } else {
        echo 'Failed to Parse JSON';
    }
} else {
    echo 'Failed with ' . $i['http_code'];
}

This should also work for subscribers (since using the broadcasters sub token), calling

https://api.twitch.tv/helix/subscriptions?broadcaster_id=CHANNELID&first=1

Instead and extracting total the same way.

This is not work. i edited user id, token and client id.
Error text;
Failed with 401

This indicates you used a token that is likely not valid to call subscriptions with

Change:

echo 'Failed with ’ . $i[‘http_code’];

to

echo 'Failed with ’ . $i[‘http_code’] . ’ - ’ . $resp;

And it’ll spit out the error

You need a user token that belongs to CHANNELID with the scope channel:read:subscriptions applied to it

iam having an authorization problem.

Failed with 401 - {"error":"Unauthorized","status":401,"message":"Invalid OAuth token"}

I’m trying to pull data for my own channel. I created cliend id, secret code.

You need to generate a token.

The Client Secret is not a token.
This error sounds like you used your clientSecret as token.
The secret is not a token.

You will need to use your ClientID and Secret to create a webpage to fetch and generate a token with the scope channel:read:subscriptions applied

As covered in this documentation:

I can’t understand what is explained in the document. I’m about to eat my head.Isn’t token generated from developer console? I can’t find the token. :rage: :rage:

No the token is not generated fron the console.

A given application doesn’t auto grant rights to a users account.

There is no Token to find as it’s not there.

This is the oAuth flow in a nutshell:

  • Create a webpage with a link as in <a href="">Login With Twitch</a>
  • This will be a link to Twitch along the lines of:

https://id.twitch.tv/oauth2/authorize?client_id=CLIENTID&redirect_uri=URLENCODEDRETURLINK&response_type=code&scope=channel:read:subscriptions

  • The user you want to read subscribers of will click this link
  • The user is then asked if they wish to allow CLIENTID access to their account.
  • The user click yes/no
  • If yes, the user is then rediredrect back to RETURNLINK with a ?code in the URL
  • The webpage at RETURNLINK then will do a POST request to Twitch to exchange that code for an access token (and a refresh token).

That access token can then be used to do sub data lookups. You will need to store the refresh token so you can get a new access token when the access token expires

This is a “one page” example in PHP that will do “regular/refreshable” User authentication that should serve as an example to build you own flow for this https://github.com/BarryCarlyon/twitch_misc/tree/main/authentication/user_access_generator/php

And example includes a script to maintain that token.

But you’ll need to determine where you store the token/refresh token for use and adjust accordingly.

The ones my made on what you sent. (github) :thinking:
{“status”:400,“message”:“invalid client”}

You need to substitute the CLIENTID for a valid ClientID.

This error suggests the ClientID you provided was invalid

There are 3 different client ids that I created. I’m getting the same error on all of them.
Since I am a new member, it does not allow me to share screenshots.

What is the outbound <a href="" you have created?

I just want to take the data of my own channel and publish it on my website.It shouldn’t be this hard! Why am I still having problems even though I have given the necessary permissions through the console. It’s so silly.

https://id.twitch.tv/oauth2/authorize?client_id=(ClientID)&redirect_uri=(MyWebSite)&response_type=code&force_verify=false&state=D1f3%2BNV1N2vlnw%3D%3D&scope=
I have authorized via this link. And the authorization I have given in the Account Settings - Connections section of my channel is visible.

(ClientID) is not a valid clientID. Hence the error…

There is no need to hide a clientID, as they are considered public.

There isn’t a permissions system in the console. Not sure to what you refer.

Which step is resulting in the error, oAuth or subdata fetching? Just to clarify

Does it create any security vulnerability if I share the Client ID with you?

The permission I am talking about is not on the console.I can view it in my twitch account as an authorized client ID.

I can’t create a token. Or I can’t access the token I created.

No. ClientID’s are considered public as they show in the URL when people authenticate.

Which step of the flow is failing?

Should I create tokens via twitchtokengenerator com? I cannot create any other tokens.

You shouldn’t use other peoples token generators.

You should build/utilize your own.

I can’t do it myself. I couldn’t create a token no matter what I did. I have no clue how to proceed.Can you help on any platform like Discord or etc.?

The token I created through the Twitch generator is only 1 day old. Can you help me create a permanent token?

I logged in as you posted this.