如何使用超出 maxResults 范围的 videoId 和 playlistId 获取 playlistItem?

How to get playlistItem using videoId and playlistId out of maxResults range?

PlaylistItem 未被 videoIdplaylistId 检索到。当不在maxResults范围内时就会出现这种情况。

你会得到项目有低于查询的数据。

GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=3&playlistId=PLLALQuK1NDrgb03P3lIgK7SrBdYbuh4_5&videoId=H4lRmVy_qYc&key={YOUR_API_KEY}

您这次无法获得物品。

GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=2&playlistId=PLLALQuK1NDrgb03P3lIgK7SrBdYbuh4_5&videoId=H4lRmVy_qYc&key={YOUR_API_KEY}

Try it here

唯一不同的是 maxResults 的数量。 playlistItem 的索引为 3,因此未检索到。

没关系,除非我尝试获取超出最大数量 maxResults 的 PlaylistItem。但我必须这样做。这让我很沮丧。

有什么解决方法吗?

API 的这种行为实际上非常令人困惑,我不希望发生这样的事情。毕竟,documentation for the maxResults parameter 声明:

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

由于视频在播放列表中出现了两次,因此应该假设 maxResults 参数设置为 2 或 3 或任何高于该值的数字的请求将 return 这两个项目。

可能的解决方法

我不太明白为什么不能将 maxResults 参数提高到 50,但这会起作用:

首先,获取播放列表中所有个视频的列表(不带videoId参数)。设置maxResults为50。这样的话,播放列表中每50个视频就得请求一次,但至少这个方法是靠谱的。确保考虑响应中的任何 nextPageToken 属性。这是获取超过 maxResults.

项的最简单方法

可选地,如果您不需要更多信息(如视频描述等),您可以将 fields 参数设置为 items(snippet(position,resourceId/videoId)),nextPageToken。这样,它只会 return 必要的信息并减少响应的大小。您的查询可能如下所示:

GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLLALQuK1NDrgb03P3lIgK7SrBdYbuh4_5&fields=items(snippet(position%2CresourceId%2FvideoId))%2CnextPageToken&key={YOUR_API_KEY}

fields 参数是使用字段编辑器在 "Try it!" 区域中组成的)

现在您已经有了播放列表中所有项目的视频 ID,只需检查您的 ID 是否在其中。