Get all subscribers in JavaScript (jQuery)

Hello, I’m trying to get all the subscribers of a stream in jQuery, but I can not.

I would like to know if you could help me with some code.

Currently I use the following code to get data like: Email, nick, date of registration, last update, banner and profile photo.

I would like to get all the subscribers of X stream.

$(function() {

	var getInfo = function(callback) {
		Twitch.api({method: 'channel'}, function(error, channel) {
		  callback(channel);
		});
	}

	 Twitch.init({clientId: 'CLIENTID'}, function(error, status) {
	 	
	 	console.log(status);

	 	if (status.authenticated) {
	 		$('.twitch-connect').hide();

	 		getInfo(function(data) {
	 			$('strong').text(data.display_name);

	 			$('#picture').attr('src', data.logo);
	 			$('#visit').text('Visita mi canal').attr('href', data.url);
	 		});
	 	} else {
	 		$('#login-info').hide();
	 	}   

	 });

	 var login = function()
	 {
	 	Twitch.login({
		  scope: ['user_read', 'channel_read', 'channel_check_subscription']
		});
	 }

	 var logout = function()
	 {
	 	Twitch.logout(function(error) {
		    $('.twitch-connect').show();

		    $('strong').text('');
		    $('#picture').attr('src','');
		    $('#visit').text('').attr('href','#');
		    
		    $('#login-info').hide();
		});
	 }

	 $('.twitch-connect').click(function(e) {
	 	e.preventDefault();

	 	login();
	 })

	 $('.twitch-disconnect').click(function(e) {
	 	e.preventDefault();

	 	logout();
	 })

})

You can only get the list of subscribers if the user gives you access to that information using one of our Authentication flows.

First, you’ll need to get an OAuth token with the correct scope for that channel. Second, you’ll call the subscriptions endpoint and pass in that OAuth token. One thing I notice is that you’re requesting a channel_check_subscription scope that doesn’t exist. Scopes are listed with the endpoint in the documentation above.

@DallasNChains Thanks for the reply! I do not know how to do with the code I already have in order to get all subscribers.

I already have oAuth authentication, just need to know how to get the list of subscribers

Are you using a Twitch API library?

To me, it seems like seems like you have to request the sub list using the same format as the getInfo variable. Instead of method: 'channel' you’d have something like method: 'subscriptions' and maybe pass the oauth token as an argument.

Btw, it may be in your best interest to secure your clientId so malicious users do not abuse/steal your id.

There is no way to get all subscriptions of a user using the official Twitch API.
Using the documented subscriptions endpoints you can:

  • Get all subscribers of a channel
  • Check if a user is subscribed to a channel (with authorization by either the user or the channel in question)

The only way to get all subscriptions of a user is the undocumented tickets endpoint:

https://api.twitch.tv/api/users/:login/tickets

I can’t remember the required scope from the top of my head, but I can check later today if you need it.

As always with undocumented endpoints: Use at your own risk. The ticket endpoint is used by the website itself as well as numerous third party services like Streamlabs and BTTV (as I said, it’s the only way to get that kind of information).

Hello!

I modified the code as I commented @while-loop hile-loop and it was as follows:

$(function() {

	var getInfo = function(callback) {
		Twitch.api({method: 'channel'}, function(error, channel)
		{
		  callback(channel);
		});
	}

	var getSubs = function(callback)
	{
		Twitch.api({method: 'subscriptions'}, function (error, subs)
		{
			callback(subs);
		});
	}

	 Twitch.init({clientId: 'sml88j76t9raxkfszgpa0p1znxdowhe'}, function(error, status) {
	 	
	 	console.log(status);

	 	if (status.authenticated) {
	 		$('.twitch-connect').hide();

	 		getInfo(function(data) 
	 		{
	 			$('strong').text(data.display_name);
	 			$('#picture').attr('src', data.logo);
	 			$('#visit').text('Visita mi canal').attr('href', data.url);
	 		});

	 		getSubs(function(data)
	 		{
	 			console.log(data);
	 		});

	 	} else {
	 		$('#login-info').hide();
	 	}   

	 });

	 var login = function()
	 {
	 	Twitch.login({
		  scope: ['user_read', 'channel_read', 'channel_subscriptions']
		});
	 }

	 var logout = function()
	 {
	 	Twitch.logout(function(error) {
		    $('.twitch-connect').show();

		    $('strong').text('');
		    $('#picture').attr('src','');
		    
		    $('#login-info').hide();
		});
	 }

	 $('.twitch-connect').click(function(e) {
	 	e.preventDefault();

	 	login();
	 })

	 $('.twitch-disconnect').click(function(e) {
	 	e.preventDefault();

	 	logout();
	 })

})

This code returns me the following:

The only thing I want to know is if the user is subscribed to a single channel, nothing more.

I would like to check if that user that logged in is subscribed to my channel.

Thank you very much.

To check if user subscribes to a channel, you must use the /users/:user/subscriptions/:channel as your method argument (Don’t forger to replace :user and :channel with the actual values.

method: '/users/the_user/subscriptions/your_channel'

Required scope: user_subscriptions

The Twitch API Documentation also describes the responses for this endpoint. A channel object is returned if the user is subscribed to the channel and a 404 Not Found error if user is not subscribed.

Also, I’m not sure if this is the library you’re using, but you should check out the examples given to perform more API requests.

@xLukii I deleted that image link since it contains an OAuth token. OAuth tokens are considered secret, and you are responsible for what happens with all OAuth tokens your application uses. Don’t want them leaking out.

@while-loop has the right idea.

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