Check if user is subscriber

Hi,

Unfortunately, I can’t get any further.

Users can log in to my website using the TwitchLogin.

The login works perfectly according to this pattern:

MY WORKFLOW

  1. Click on the button (https://id.twitch.tv/oauth2/authorize)
  2. Get Code
  3. Get Token from https://id.twitch.tv/oauth2/token
  4. Get user data with tokens from https://api.twitch.tv/helix/users

But now I would like to check whether the user is a subscriber in my channel.

After hours of searching on Google and here in the forum, I only found the information that I can only read all subscribers from my channel via https://api.twitch.tv/helix/subscriptions?broadcaster_id=XXXXXXXX

However, that doesn’t do me any good, as I only get this information when I click the login button and a token is generated for me.

But the user clicks the login button and then receives a token for himself.

Do I now have to log in every day in order to save the subscribers in my database and check them each time the user logs in?

It should also go over the old API V5 (Kraken). However, this somehow doesn’t work in my process.

I had tried this in my workflow (tokens etc. above): https://api.twitch.tv/kraken/users/#USERID#/subscriptions/#CHANNELID#

But then do I only get an empty array back?

Can someone help me and tell me where the problem is?

I know you probably post links to the docs of the API V5 and the new Api. Please don’t do that, I already know all of them from all other forum posts and from all stackoverflow posts. That doesn’t get me any further as I seem to have a mistake in my thoughts somewhere.

Many Thanks!

Step 1) Setup an oAuth loop to get a token for yourself that will read subscribers
Step 2) Store that token and the refresh token in a database
Step 3) user logs into your website as them
Step 4) use the users token to get their user ID
Step 5) call the subscribers API with their userID and your userID (as the broadcasterID) but your oAuth token that you load up from the database.

You would store your oAuth token (and refresh token) in a database and recall it/refresh it as needed.

You have all the right parts. You just didn’t connect the dots.

The token you generate for yourself will be valid for four hours, but because you have a refresh token, you can refresh it as needed whenever you need it. Then store the new token back in the database for later use.

No you just have to login/create your oAuth token with subscriber access whenever the refresh token procedure fails, which should only be if you disconnect your own app, or your reset your password.

1 Like

Hi,

what a great answer. Thank you very much!

Hi Barry,

I still have an important question and I hope you can help me.

Do I have to get a new token here https://id.twitch.tv/oauth2/token (start new call) every time I query https://api.twitch.tv/helix/subscriptions? Or can i only use the token from login via https://id.twitch.tv/oauth2/token from database? Can I call https://api.twitch.tv/helix/subscriptions again and again until the token has expired?

I am asking because I am now getting a 401 error. When I call https://api.twitch.tv/helix/subscriptions from the broadcaster with the token and ClientID.

My Code:

$curl = curl_init('https://api.twitch.tv/helix/subscriptions?broadcaster_id='.$broadcasterClientID);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'Client-ID: ' . $broadcasterClientID,
      'Authorization: Bearer ' . $broadcasterToken
  ));
  curl_setopt($curl, CURLOPT_TIMEOUT, 1000);    

Many Thanks!

Yes.

When the token expires, use the refresh token to get a new token

Read the body message of the response it’ll tell you the problem.

HTTP Code’s are useless without the body messages from the response

Ok, thanks for the info.

Oops, forgot the body. I get this information:

{“error”:“Unauthorized”,“status”:401,“message”:“Client ID and OAuth token do not match”}

The ClientID is the ID that I got back from the login as the id in the object.

An idea?

Ah no stop it all back. The ClientID is of course the ID of the Twitch application. I think that’s the mistake.

Ok that was the problem. I am stupid ^^
Do i only get 20 subscribers? In Doc there are two parameters first and after. But how are these to be used as parameters in the url?

Aaah ok, so you can use the following (for everyone who has the same problem)

20 results
https://api.twitch.tv/helix/subscriptions?broadcaster_id=$broadcasterID

100 results
https://api.twitch.tv/helix/subscriptions?broadcaster_id=$broadcasterID&first=100

Specifically a user
https://api.twitch.tv/helix/subscriptions?broadcaster_id=$broadcasterID&user_id=$user_twitchID

But what is the parameter After?

https://api.twitch.tv/helix/subscriptions?broadcaster_id=$broadcasterID&after=100

https://api.twitch.tv/helix/subscriptions?broadcaster_id=$broadcasterID&after=1

don’t work. But why?

after uses the value of pagination->cursor

For example

1 Like

aah ok so i use it like:
https://api.twitch.tv/helix/subscriptions?broadcaster_id=$broadcasterID&after=eyJiIjp… ?

Correct

1 Like

Hey barry,

do you know whether it is possible to read a user’s follower date? Or is that not possible via the API?

Thank you !

Thats included in

Example

{
   "total": 12345,
   "data":
   [
      {
         "from_id": "171003792",
         "from_name": "IIIsutha067III",
         "to_id": "23161357",
         "to_name": "LIRIK",
         "followed_at": "2017-08-22T22:55:24Z"
      },
      {
         "from_id": "113627897",
         "from_name": "Birdman616",
         "to_id": "23161357",
         "to_name": "LIRIK",
         "followed_at": "2017-08-22T22:55:04Z"
      },
      ...
   ],
   "pagination":{
     "cursor": "eyJiIjpudWxsLCJhIjoiMTUwMzQ0MTc3NjQyNDQyMjAwMCJ9"
   }
}

You can specify a from_id (user) and to_id (broadcaster) to get the date that somesuer followed lirik on Twitch. (Names for example to illustrate relationship)

1 Like

Thank you very much!

So I have to go through all followers to see if a user followed. I can’t search for the UserID directly, right?

Ah ok i see i can use both params nice !

https://api.twitch.tv/helix/users/follows?from_id=$user_userid&to_id=$masterBroadcasterID

I did say and :smiley:

Edit: you can also to/from Pairs for the subscribers/banned endpoints etc

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