Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

youtube.subscriptions.list (api v3) - nextPageToken isn't available

I'm trying to get all channels from my subscriptions. But the "nextPageToken" isn't available.

The response should containing "nextPageToken":

(from developers.google.com - YouTube (v3) - Subscriptions: list)

{
  "kind": "youtube#subscriptionListResponse",
  "etag": etag,
  "nextPageToken": string,
  "prevPageToken": string,
  "pageInfo": {
  "totalResults": integer,
  "resultsPerPage": integer
  },
  "items": [
    subscription Resource
  ]
}

This is my request:

GET https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&maxResults=10&mine=true&key={YOUR_API_KEY}

APIs Explorer - YouTube (v3) - Subscriptions.list:

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.list?part=snippet&maxResults=10&mine=true

My response:

{
 "kind": "youtube#subscriptionListResponse",
 "etag": "\"XXXXX/XXXXX\"",
 "pageInfo": {
  "totalResults": 115,
  "resultsPerPage": 10
 },
 "items": [
  ...

Can you tell me why the nextPageToken is missing, please?

like image 463
Fleckchen Avatar asked May 15 '15 15:05

Fleckchen


2 Answers

I have now a workaround for this. Please tell me if that helps.

The tokens seems to be the same for each page of other API Youtube V3 API calls, so I can use it to fetch all pages of subscriptions I need.

tokens = ['CDIQAA','CGQQAA','CJYBEAA','CMgBEAA','CPoBEAA','CKwCEAA','CN4CEAA','CJADEAA','CMIDEAA','CPQDEAA','CKYEEAA', ...]

You can use ANOTHER Youtube API to get more page tokens if you need more. Just fetch 1 element a time and log the tokens to use in this API.

I just need to known when to stop... so I checked when API calls returned no channels!

@retry(stop_max_attempt_number=7)    
def get_subscription_page(self, channel_id, pageToken):        
    print 'Retrieving subscription page using Youtube API (token: %s)' % pageToken
    res = self.youtube_data_api.subscriptions().list(part="id,snippet,contentDetails",channelId=channel_id, maxResults=50, pageToken=pageToken).execute()
    return res


def get_subscriptions(self, channel_id):
    self.authorize(channel_id)                       
    subs = []         

    # Tokens to deal with api bug...
    # https://code.google.com/p/gdata-issues/issues/detail?id=7163
    tokens = ['CDIQAA','CGQQAA','CJYBEAA','CMgBEAA','CPoBEAA','CKwCEAA','CN4CEAA','CJADEAA','CMIDEAA','CPQDEAA','CKYEEAA']

    iPage = 0
    pageToken = ''        
    while True:            

        res = self.get_subscription_page(channel_id, pageToken)            

        channelIds = []
        for channel in res['items']: channelIds.append(channel.get('snippet').get('resourceId').get('channelId'))
        pageToken = res.get('nextPageToken')

        # If no next page token is returned... it might be caused by a bug.
        # This workaroud will only have effect when the bug still lives.
        if not pageToken:
            if not channelIds:
                # Workaroud for this: https://code.google.com/p/gdata-issues/issues/detail?id=7163
                print ' (Workaround due to API bug) No channels returned in this API call! Finished!'
                break
            else:
                pageToken = tokens[iPage]

        # get channel info for each channel ID
        channelsInfo = self.get_channel_info(channelIds)            
        subs += channelsInfo
        print ' Itens already retrieved: %d ' % len(subs)                                                            
        iPage += 1

        if args.debug: break
        if pageToken: continue

        print 'No more pages to retrieve!'
        break                

    return subs
like image 177
Alex Benfica Avatar answered Oct 03 '22 14:10

Alex Benfica


Here is a JS snippet I came up with to generate pageTokens up to at least 1024, I cannot guarantee that it will produce anything valid beyond that as i could not find any service which will get me tokens for offsets > 450 to validate my guesses and assumptions.

var d0 = "AEIMQUYcgkosw048";
var d1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var d2 = d1;
var d1c = 0;
var d2c = 0;
var overflowSuffix = "Q";
var direction = "AA";
var d2OverflowCounter = 0;
var pageSize = 50;

for (i = 0; i < 1024; i++) {
  if (i % pageSize == 0) console.log("C" + d1.charAt((d1c / d0.length) % d1.length) + d0.charAt(i % d0.length) + overflowSuffix + direction, ":", i);
  if (++d1c % (1 << 8) == 0) d1c = 1 << 7;
  if (++d2c % (1 << 7) == 0) overflowSuffix = d2.charAt(++d2OverflowCounter) + "E";
}

(check developer tools / console to see generated codes)

like image 27
Paul Rogalinski pulsar Avatar answered Oct 03 '22 15:10

Paul Rogalinski pulsar