Retrieve all videos from youtube playlist using youtube v3 API

Im retrieving videos of a playlist using youtube v3 API and getting 50 items without any problem with this link:-

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLB03EA9545DD188C3&key=MY_API_KEY

But the video count is 100 and im only getting 50. How can i get the next 50 items? I tried start-index but it doesnot work for v3 API. Any help is appreciated.


YouTube Data API v3 results are paginated. So you need to get the next page of results for the others.

Basically in the response you have nextPageToken.

To get the remaining results, do the same exact call but setting pageToken into that token you received.


Ther are three tokes

  • pageToken
  • nextPageToken
  • prevPageToken
  • and also you can set max page size using

    maxResults=50 {allowed Values 1 to 50 }

    if you are in page 1 you won't get prevPageToken

    but you get nextPageToken

    pass this token to next request's

    pageToken = {nextPageToken get from last request}

    this way you can navigate to next page Try it YourSelf

    Edited

    Ok, for other scenarios

    If you are in any other page not is firs or last then there will be all these values

  • pageToken = 'Some values'
  • nextPageToken = 'Some values'
  • prevPageToken = 'Some values'
  • @Manoj : you can find your answer below if you are in last page

  • pageToken = 'Some values'
  • nextPageToken = 'Some values'
  • prevPageToken = null

  • This is a small example made in python using the Python Youtube Client Lib This also borrows boilerplate setup from the youtube API examples

    """ Pull All Youtube Videos from a Playlist """
    
    from apiclient.discovery import build
    from apiclient.errors import HttpError
    from oauth2client.tools import argparser
    
    
    DEVELOPER_KEY = "YOURKEY HERE"
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"
    
    def fetch_all_youtube_videos(playlistId):
        """
        Fetches a playlist of videos from youtube
        We splice the results together in no particular order
    
        Parameters:
            parm1 - (string) playlistId
        Returns:
            playListItem Dict
        """
        youtube = build(YOUTUBE_API_SERVICE_NAME,
                        YOUTUBE_API_VERSION,
                        developerKey=DEVELOPER_KEY)
        res = youtube.playlistItems().list(
        part="snippet",
        playlistId=playlistId,
        maxResults="50"
        ).execute()
    
        nextPageToken = res.get('nextPageToken')
        while ('nextPageToken' in res):
            nextPage = youtube.playlistItems().list(
            part="snippet",
            playlistId=playlistId,
            maxResults="50",
            pageToken=nextPageToken
            ).execute()
            res['items'] = res['items'] + nextPage['items']
    
            if 'nextPageToken' not in nextPage:
                res.pop('nextPageToken', None)
            else:
                nextPageToken = nextPage['nextPageToken']
    
        return res
    
    if __name__ == '__main__':
      # comedy central playlist, has 332 video
      # https://www.youtube.com/watch?v=tJDLdxYKh3k&list=PLD7nPL1U-R5rDpeH95XsK0qwJHLTS3tNT
      videos = fetch_all_youtube_videos("PLD7nPL1U-R5rDpeH95XsK0qwJHLTS3tNT")
    

    videos will be a list of all your videos concatenated to the first list. It will keep fetching until it has all the videos because of pagination by 50. A similar approach can be taken in other languages.

    In the list there will be all the individual video meta data and order

    链接地址: http://www.djcxy.com/p/28950.html

    上一篇: 是否有可能通过Youtube Api创建我自己的YouTube播放列表

    下一篇: 使用youtube v3 API检索YouTube播放列表中的所有视频