通过 GET 请求获取一个 YouTube 播放列表的所有视频 ID

Get all video IDs of a YouTube playlist through GET requests

我正在尝试加载 YouTube 播放列表的所有视频 ID 并将它们保存在一个数组中以便稍后访问它们。

我从这个开始:

$.getJSON( "https://www.googleapis.com/youtube/v3/playlistItems", {
  part : 'snippet', 
  playlistId : inputId,
  key: API_KEY
}, function(data) {
  $.each(data.items, function(i, item) {
    var itemId = item.snippet.resourceId.videoId;
    videoIds.push(itemId);
  }
);

然后我注意到我的数组中只保存了输入播放列表的前 5 个视频 ID 'videoIds'。显然,YouTube API 使用页面来限制 GET 响应中的项目数量。我没有看到增加每个请求的项目数量的方法。因此,我改为使用响应键 'resultsPerPage'、'totalResults' 和 'nextPageToken' 更新我的代码。基本上,我现在为播放列表的每一页发送一个 GET 请求:

$.getJSON("https://www.googleapis.com/youtube/v3/playlistItems", {
  part : 'snippet', 
  playlistId : inputId,
  key: API_KEY
}, function(data) {
  resultsPerPage = data.pageInfo.resultsPerPage;
  nextPageToken = data.nextPageToken;
  totalResults = data.pageInfo.totalResults;
  $.each(data.items, function(i, item) {
    var itemId = item.snippet.resourceId.videoId;
    videoIds.push(itemId);
  });

  // if playlist items don't fit on a single page
  if (resultsPerPage < totalResults) {
    for (var i = 0; i < totalResults/resultsPerPage; i++) {
      // send the request again...
      $.getJSON("https://www.googleapis.com/youtube/v3/playlistItems", {
        part: 'snippet', 
        playlistId: inputId,
        // but this time, with a pageToken
        pageToken: nextPageToken,
        key: API_KEY
      }, function(data) {
        // debug logging  
        console.log("old token:" + nextPageToken);
        console.log("new token:" + data.nextPageToken);
        // update the nextPageToken for the next iteration
        nextPageToken = data.nextPageToken;
        $.each(data.items, function(i, item) {
          var itemId = item.snippet.resourceId.videoId;
          videoIds.push(itemId);
        });
      });
    }
  }
});

我的问题:'nextPageToken' 的值永远不会改变。这是我 运行 我的代码后我的控制台的样子:

old token:CAUQAA script.js:62 
new token:CAoQAA script.js:63
old token:CAoQAA script.js:62
new token:CAoQAA script.js:63
old token:CAoQAA script.js:62
new token:CAoQAA script.js:63
old token:CAoQAA script.js:62
new token:CAoQAA script.js:63

为什么 GET 响应中 nextPageToken 的值与我在相关 GET 请求中用于 pageToken 的值相同?有没有更简单的方法来获取播放列表的所有 ID?

YouTube 数据 API 的 Videos.list API endpoint returns maximum 50 items per page:

maxResults (unsigned integer)

The maxResults parameter specifies the maximum number of items that should be returned in the result set.

Note: This parameter is supported for use in conjunction with the myRating parameter, but it is not supported for use in conjunction with the id parameter. Acceptable values are 1 to 50, inclusive. The default value is 5.

nextPageToken的工作方式是这样的:

情况:

您正在尝试检索包含 200 个视频的列表,但 YouTube 每个分页只能 return 50 个结果。

  1. 第一次请求成功后,YouTube 会为您提供前 50 个视频,因此只剩下 150 个。前 50 个视频列表附带一个 nextPageToken,您可以使用它来检索下一个 51-100。

  2. 所以您将此 nextPageToken 值传递给 pageToken 属性 并执行另一个请求,这导致 YouTube 数据 API 为您提供接下来的 51-100 个视频,所以只剩下 100 个了。随着这 51-100 个视频又出现了一个新的 nextPageToken,它将用于检索接下来的 101-150 个视频。冲洗并重复。

通过有关如何执行此操作的实际演示,这会更容易,因此请观看 this tutorial

Google API Explorer youtube.search.list.

上测试