YouTube数据API搜索问题,下一页结果包含上一页

发布于 2025-01-25 14:56:43 字数 982 浏览 1 评论 0原文

  • 我使用了youtube数据API返回搜索结果。为了获得更多结果,我将NextPageToken用作param。 =>当我进行BTN时,请单击,显示下一页的5个视频。
  • 它实际上有效,但是某些下一页结果包含上一页的视频。
  • 我希望如果第三页页面视频是 a ,b,c,d,e,第四页项目是f,g,h,i,i,j。
  • 但是第四页类似于 a ,f,g,h,i或其他东西。我不确定是否有任何规则。
search = async (input) => {
    const response = await fetch(
      `https://youtube.googleapis.com/youtube/v3/search?part=snippet&q=${input}&type=video&maxResults=15&key=${this.key}`,
      this.requestOptions
    );
    return await response.json();
  };

loadMore= async (searchToken, inputValue) => {
    const response = await fetch(
      `https://youtube.googleapis.com/youtube/v3/search?part=snippet&q=${inputValue}&type=video&pageToken=${searchToken}&key=${this.key}`,
      this.requestOptions
    );
    return await response.json();
  };

是否有人经历过并解决了这些问题?

  • I used youtube data api returning search result. And to get more result, I used nextPageToken as param. => when a btn i made clicked, showing next page's 5 videos.
  • It actually works, but some next page results contain previous page's videos.
  • I expected if 3rd page videos are a, b, c, d, e, 4th page items are f, g, h, i, j.
  • but 4th page's like a, f, g, h, i or something. I'm not sure there is any rules or not.
search = async (input) => {
    const response = await fetch(
      `https://youtube.googleapis.com/youtube/v3/search?part=snippet&q=${input}&type=video&maxResults=15&key=${this.key}`,
      this.requestOptions
    );
    return await response.json();
  };

loadMore= async (searchToken, inputValue) => {
    const response = await fetch(
      `https://youtube.googleapis.com/youtube/v3/search?part=snippet&q=${inputValue}&type=video&pageToken=${searchToken}&key=${this.key}`,
      this.requestOptions
    );
    return await response.json();
  };

has anyone experienced and solved this problems?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2025-02-01 14:56:43

YouTube API以分页的方式返回响应。这意味着,如果您使用搜索功能,则您的搜索结果将在每个页面具有不同页面令牌的不同页面上可用。 MaxResults参数确定每个页面上的结果数(默认值= 50)。要解决此问题并在每个呼叫中​​返回新/不同的响应,请将 NextPageToken 传递给您的下一个API呼叫。

例如,如果您的第一个API呼叫看起来像这样:

GET https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=10&q=cricket&key=[YOUR_API_KEY]

您的API响应看起来像:

{
"kind": "youtube#searchListResponse",
"etag": "uN1c33JfiFaPBemlxN5kH8lSaHw",
"nextPageToken": "CAoQAA",
"regionCode": "IN",
"pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 10
},

获得1000000的下10个结果,请添加Pagetoken = nextPagetoken 在您的查询中,类似的东西:

GET https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=10&pageToken=CAoQAA&q=cricket&key=[YOUR_API_KEY]

要 呢

{
"kind": "youtube#searchListResponse",
"etag": "NeaA5DLyr3YIaKdX5ZxETA3GfhY",
"nextPageToken": "CBQQAA",
"prevPageToken": "CAoQAQ",
"regionCode": "IN",
"pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 10
}

您将获得接下来的10个结果!

第一页不会有任何pagetoken,因此,如果没有pagetkoken参数,则需要进行第一个API呼叫

请参阅官方文档以获取更多详细信息:
https://developers.google.com/youtube/youtube/v3/docs/v3/docs/search/search/列表

The Youtube API returns the response in a paginated manner. This means that if you use the search functionality, your search results will be available on different pages where each page has a different page token. The maxResults parameter determines the number of results on each page(default=50). To tackle this problem and return new/different responses with each call , pass the nextPageToken to your next API call.

For example, if your first API call looks like this :

GET https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=10&q=cricket&key=[YOUR_API_KEY]

Your API response would look like :

{
"kind": "youtube#searchListResponse",
"etag": "uN1c33JfiFaPBemlxN5kH8lSaHw",
"nextPageToken": "CAoQAA",
"regionCode": "IN",
"pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 10
},

To get the next 10 results of those 1000000, add pagetoken = nextPageToken to your query ,something like this :

GET https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=10&pageToken=CAoQAA&q=cricket&key=[YOUR_API_KEY]

AND VOILA!

{
"kind": "youtube#searchListResponse",
"etag": "NeaA5DLyr3YIaKdX5ZxETA3GfhY",
"nextPageToken": "CBQQAA",
"prevPageToken": "CAoQAQ",
"regionCode": "IN",
"pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 10
}

YOU GET THE NEXT 10 RESULTS!

THE FIRST PAGE WOULD NOT HAVE ANY PAGETOKEN, SO THE FIRST API CALL NEEDS TO BE MADE WITHOUT PAGETOKEN PARAMETER

Refer to the official documentation for more details:
https://developers.google.com/youtube/v3/docs/search/list

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文