使用youtube v3 API检索YouTube播放列表中的所有video

即时通讯使用YouTube的V3 API获取播放列表的video,并获得50项没有任何问题,这个链接: –

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

但video计数是100,即时只获得50.我怎样才能得到接下来的50个项目? 我尝试启动索引,但它不适用于V3 API。 任何帮助表示赞赏。

YouTube数据API v3结果分页。 所以你需要得到其他人的结果的下一页。

基本上在你有nextPageToken的回应。

要获得剩余结果,请执行相同的确切呼叫,但将pageToken设置为您收到的令牌。

这是三个tokes

  1. pageToken
  2. 的nextPageToken
  3. prevPageToken

也可以使用设置最大页面大小

maxResults = 50 {允许值1至50}

如果你在页面1,你将不会得到prevPageToken

但你得到nextPageToken

将此令牌传递给下一个请求

pageToken = {nextPageToken get last request}

这样你可以导航到下一页尝试YourSelf

编辑

好的,其他情况

如果你在任何其他页面不是冷杉或最后那么将有所有这些值

  1. pageToken ='有些值'
  2. nextPageToken ='有些值'
  3. prevPageToken ='有些值'

@Manoj:如果你在最后一页,你可以在下面find你的答案

  1. pageToken ='有些值'
  2. nextPageToken ='有些值'
  3. prevPageToken = null

这是使用Python Youtube Client Lib在python中做的一个小例子。这也借用了youtube API示例中的样板设置

 """ 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") 

video将成为连接到第一个列表的所有video的列表。 它将继续提取,直到它有50个分页的所有video。类似的方法可以采取其他语言。

在列表中将会有所有的个人video元数据和订单

这个javascript检索115个剪辑(来自PLTI6yRvQqlYq9KoU-NHu43uDmKON7Fsjv)和91个剪辑(来自PL32C69B40337EF920)
testing这个HTML文件在:
http://pvhung20.url.ph/api3/retrieve-all-videos-stackoverflow.html

 sum = 0; sumN = 1; var nextPageToken; function getVids(PageToken){ pid = $('#searchtext1').val(); $.get( "https://www.googleapis.com/youtube/v3/playlistItems",{ part : 'snippet', maxResults : 50, playlistId : pid, pageToken : PageToken, key: 'YOUR API3 KEY' }, function(data){ myPlan(data); } ); } function myPlan(data){ total = data.pageInfo.totalResults; nextPageToken=data.nextPageToken; for(i=0;i<data.items.length;i++){ document.getElementById('area1').value += sumN + '-' + data.items[i].snippet.title+'\n'+ data.items[i].snippet.resourceId.videoId +'\n\n'; sum++ ; sumN++; if(sum == (total-1) ){ sum = 0; return; } } if(sum <(total-1)){ getVids(nextPageToken); } } function init(){ $('#area1').val(''); } 
 <meta http-equiv="content-type" content="text/html; charset=utf-8"> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <body onload="$('#area1').val('')"> <input type="text" value="PLTI6yRvQqlYq9KoU-NHu43uDmKON7Fsjv" id="searchtext1" size="75">&nbsp; <button onclick="getVids()">Get Items</button> <br><br> IDs for test: <br>PLTI6yRvQqlYq9KoU-NHu43uDmKON7Fsjv<br> PL32C69B40337EF920 <br><br> <textarea id="area1" style="width:600px;height:500px"> </textarea> 

另一个解决scheme,使用recursion:

 $.fn.loadYoutubeResource = function(resource_request, resource_type, resource_id, resource_container, pageToken = null, callback = null){ $.ajax({ url: "https://www.googleapis.com/youtube/v3/" + resource_request, type: 'get', dataType: 'json', data: { part : 'snippet', [resource_type]: resource_id, maxResults : 50, pageToken: pageToken, key: '< API Key >', }, success: function(data) { console.log("New resource " + resource_type + " loaded:"); console.log(data); for(var index = 0; index < data.items.length; index++){ var url = data.items[index]['snippet'].thumbnails.default.url; var ytb_id = data.items[index]['id']; jQuery('#' + resource_container).append('<img class="tube_thumbs" src="' + url + '" id="' + ytb_id + '" title="' + data.items[index]['snippet']['title'] + '" >'); } if ( data.nextPageToken == null) return callback(); $.fn.loadYoutubeResource(resource_request, resource_type, resource_id, resource_container, data.nextPageToken, callback); } }); } 

然后调用它如下:

 jQuery('body').append('<div id="ytb_container"></div>'); $.fn.loadYoutubeResource('playlistItems', 'playlistId', 'PLmwK57OwOvYVdedKc_vPPfbcsey_R0K8r', 'ytb_container', null, function(){ <callback code>});