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

不羁岁月 提交于 2019-12-18 03:43:11

问题


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?


回答1:


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)




回答2:


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



回答3:


I have a script that runs each hour based on this Youtube API V3 and it stopped to work 4 hours ago. The nextPageToken is not available anymore. Before, it was available exactly like in the first code you posted.



来源:https://stackoverflow.com/questions/30263293/youtube-subscriptions-list-api-v3-nextpagetoken-isnt-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!