haliltprkk
haliltprkk

Reputation: 1508

Getting channel subscriptions list by Youtube Data Api

Is there any way to order the subscription list of Youtube channels by recently subscribed?

youtube
       .subscriptions()
       .list("snippet")
       .setOrder("")// relevance, unread, alphabetical
       .setMaxResults((long) 1000) // it is not affecting, the max limit is 50
       .setMine(true)
       .execute();

According to documents, I can only get max 50 items at a time, and I have only three order type parameters relevance, unread, alphabetical.

But I need to reach the channel I subscribed lastly. I would be really appreciated it if anybody helps me to handle this.

Thanks in advance!

Upvotes: 1

Views: 2233

Answers (2)

Emin Ayar
Emin Ayar

Reputation: 1106

As I understand from your question, you want to check if you are following a specific youtube channel with help of Youtube Data API V3.

For that it is mentioned in the document that you can use forChannelId parameter.

Also Youtube Data API has a playground to let you see the results of your query. You can simply put a channelId in forChannelId field and result will return an empty array if you are not subscribed specified channel or result will return the data of that specified channel if you are subscribed to it.

You can do a simple request from your Java app to get results. In this code example I'm checking if authorized youtube API user is subscribed to Firebase Youtube Channel or not.

SubscriptionListResponse response = request.setForChannelId("UC_x5XG1OV2P6uZZ5FSM9Ttw")
        .setMine(true)
        .execute();

And response will include details of specified channel in the request you will make. I also share response of the request I shared above.

{
  "kind": "youtube#SubscriptionListResponse",
  "etag": "zCQ7lTwIBgdyVsQmbymEu-fUgjU",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#subscription",
      "etag": "A-G_B0BnSqn7XtJi7BgHJEk9L3Q",
      "id": "uTEDDg6jpPBwnsim9moHkataEljshwFopudOgIy34nk",
      "snippet": {
        "publishedAt": "2020-07-08T14:02:43.789000Z",
        "title": "Google Developers",
        "description": "The Google Developers channel features talks from events, educational series, best practices, tips, and the latest updates across our products and platforms.",
        "resourceId": {
          "kind": "youtube#channel",
          "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw"
        },
        "channelId": "UCC77fYySvfP7p-6QGaa-3lw",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/-Fgp8KFpgQqE/AAAAAAAAAAI/AAAAAAAAAAA/Wyh1vV5Up0I/s88-c-k-no-mo-rj-c0xffffff/photo.jpg"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/-Fgp8KFpgQqE/AAAAAAAAAAI/AAAAAAAAAAA/Wyh1vV5Up0I/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
          },
          "high": {
            "url": "https://yt3.ggpht.com/-Fgp8KFpgQqE/AAAAAAAAAAI/AAAAAAAAAAA/Wyh1vV5Up0I/s800-c-k-no-mo-rj-c0xffffff/photo.jpg"
          }
        }
      }
    }
  ]
}

Upvotes: 1

stvar
stvar

Reputation: 6975

According to the docs, you have the following parameter at your disposal:

myRecentSubscribers (boolean)

This parameter can only be used in a properly authorized request. Set this parameter's value to true to retrieve a feed of the subscribers of the authenticated user in reverse chronological order (newest first).

Note that this parameter only supports retrieval of the most recent 1000 subscribers to the authenticated user's channel. To retrieve a complete list of subscribers, use the mySubscribers parameter. That parameter, which does not return subscribers in a particular order, does not limit the number of subscribers that can be retrieved.

That is: do insert something like .setMyRecentSubscribers(true) in the sequence of setters of your code above. (Also you may remove the setChannelId setter call, since, by requiring from you to be authorized to invoke this endpoint, the API already knows the channel to which your call is referring to.)

Note also that the parameter's maxResults maximum value is 50. To receive only the most recent subscriber have .setMaxResults(1) in the setter sequence above.


If your want to obtain the list of all your subscriptions then there's the following parameter:

mine (boolean)

This parameter can only be used in a properly authorized request. Set this parameter's value to true to retrieve a feed of the authenticated user's subscriptions.

Have .setMine(true) (without .setChannelId) in your setters sequence.

You will have to invoke repeatedly the API's endpoint to get all of your subscriptions, since this endpoint provides paginated result sets. Upon obtaining all those subscriptions, sort them by snippet.publishedAt.

If you're only interested to obtain the most recent channel to which to have subscribed, instead of the sort algorithm, is sufficient to use the max algorithm (O(n) instead of O(n log n)) on the same property.

For an example of how to implement pagination in your code, have a look at some of the sample code provided by Google itself.

Upvotes: 2

Related Questions