Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...