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

前端 未结 3 686
广开言路
广开言路 2020-12-16 05:38

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

The response should containing \"nextPageToken\":

(from de

3条回答
  •  再見小時候
    2020-12-16 05:55

    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
    

提交回复
热议问题