来自 YouTube 数据的意外 pageInfo API v3

Unexpected pageInfo from YouTube Data API v3

我尝试使用 YouTube 数据 API v3 (https://developers.google.com/youtube/v3/docs/search/list) 搜索视频。
我成功获得了一些信息,但我发现 pageInfo 的结果不是我所期望的。

这是查询和我得到的响应:

https://www.googleapis.com/youtube/v3/search?part=id&channelId=UC5fslQUCCo3OE1EaA1n5ZmQ&key=...&publishedAfter=2019-01-15T08:09:38Z&publishedBefore=2019-01-15T10:09:38Z&order=date .

{
  "kind": "youtube#searchListResponse",
  "etag": "iLvO2LgGDksqADNU-_xPwG4QMCU",
  "nextPageToken": "CAUQAA",
  "regionCode": "JP",
  "pageInfo": {
    "totalResults": 49,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "Adtll-9lbQDiKGJEkBb2IEjPfjw",
      "id": {
        "kind": "youtube#video",
        "videoId": "d4Z7MZo3-Ac"
      }
    }
  ]
}

我将日期字符串指定为 publishAfterpublishBefore,以便结果仅包含单个视频信息。
我猜 pageInfo.totalResultpageInfo.resultPerPage 都是 1.
与我的预期相反,pageInfo.totalResult 是 49,pageInfo.resultsPerPage 是 5。

什么描述了pageInfo?我的理解有误吗?

以下是您的两个问题的答案:

1.为什么pageInfo.resultsPerPage5

这个属性的规格如下:

pageInfo.resultsPerPage (integer)
The number of results included in the API response.

可以将此规范理解为(我改写上面的文字):从 API 获得的结果集中的项目数,即 JSON 数组的元素数 items.

不幸的是,这与现实不符。根据我的经验,pageInfo.resultsPerPage 只是请求参数的值 maxResults:

maxResults (unsigned integer)
The maxResults parameter specifies the maximum number of items that should be returned in the result set. Acceptable values are 0 to 50, inclusive. The default value is 5.

由于你上面的API调用URL没有指定maxResults,所以这个参数被认为是默认值;因此 pageInfo.resultsPerPage 变为 5.

再说一句:这个属性的官方说明确实有误导性;此问题很可能会作为文档错误报告给 Google(通过其 own issue tracker site)。

2。为什么 pageInfo.totalResults49 而不是 1

这个属性的规格如下(下面的重点是我的):

pageInfo.totalResults (integer)
The total number of results in the result set. Please note that the value is an approximation and may not represent an exact value. In addition, the maximum value is 1,000,000.

You should not use this value to create pagination links. Instead, use the nextPageToken and prevPageToken property values to determine whether to show pagination links.

根据本规范,pageInfo.totalResults 不能作为用户可以依赖的精确数量。 Google 的工作人员is also confirmed officially 的这一特点。

此外,如果对您的 URL 进行一些试验,消除两个日期请求参数 publishedAfterpublishedBefore,您将看到 pageInfo.totalResults 的值变得不稳定:连续多次重复相同的 API 调用(每次仅相隔几秒钟),我从来没有从 API 中得到相同的值,但是,交替 13741375.