5

I want to be able to detect if the currently authenticated user is subscribed to a specific YouTube channel in the YouTube API v3.

A possible solution would be to retrieve a list of all the subscriptions of the currently authenticated user and check if the channel ID of the channel is contained in that list. That would would be a very inefficient solution and could take a very long time if the user has hundreds of subscriptions.

Is there any easy method to check this? I looked through the entire API documentation and I couldn't find anything.

JonasG
  • 9,035
  • 12
  • 53
  • 88

2 Answers2

11

Use the subscriptions#list method and pass mine = true and the channel ID you want to check in forChannelId. If the authenticated user is not subscribed to that channel, it will return an empty list.

Vinicius Pinto
  • 7,848
  • 3
  • 40
  • 58
  • @Vinicius Pinto would you please have a look at this question? it's about the same issue. http://stackoverflow.com/questions/28189354/get-youtube-channel-subscribers-via-youtube-api – PHP User Jan 28 '15 at 16:42
  • @JonasG I'm trying to do the same thing after authenticating the user and get the access token then redirected to a link like this http://domain.com/google/test.php?code=access token here than $data =file_get_contents('https://www.googleapis.com/youtube/v3/subscriptions?part=snippet &maxResults=50&access_token='.$code.'&mine=true&key=My API Key Here'); var_export($data) But i get "false" and the error in this question http://stackoverflow.com/questions/28215693/list-user-subscriptions-to-all-youtube-channels-after-getting-access-token Hope you can help with this. Thanks in advance. – PHP User Jan 31 '15 at 08:30
0
checkSubscribe (params) {
  var request = 
gapi.client.youtube.subscriptions.list(removeEmptyParams(params));
  request.execute((response) => {
  console.log(response);
   if (response.code !== 401 && response.code !== 400 && response.items[0] ) {
      console.log('response');
      console.log(response);
     }
    });
 }
  removeEmptyParams(params)[![enter image description here][1]][1]{
    for (const pra in params) {
      if (!params[pra] || params[pra] === 'undefined') {
        delete params[pra];
     }
   }
    return params;
  }

checkSubscribe(
    {part: 'snippet, contentDetails', mine: true},
    {'forChannelId':'PUT-YOUR-CHANEL--ID','onBehalfOfContentOwner': ''}
);
D V Yogesh
  • 1,707
  • 1
  • 17
  • 34