首页 > 解决方案 > Youtube apirelatedToVideoId 给出的视频数量不正确

问题描述

我需要为 youtube iFrame 实现自定义自动播放功能,所以我决定使用youtube api relatedToVideoId 功能

问题是,由于某种原因,通常当您尝试仅获取 1 个相关视频时,它根本不会提供任何视频,即使它说结果中有很多视频。

示例请求:

https://www.googleapis.com/youtube/v3/search?key=[YOUR_KEY]&maxResults=1&part=snippet&type=video&relatedToVideoId=DlFmfxACvig

回复:

{
    "kind": "youtube#searchListResponse",
    "etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/ShTuZSfQoay1YL6jgovx1wFiJSA\"",
    "nextPageToken": "CAEQAA",
    "regionCode": "KG",
    "pageInfo": {
        "totalResults": 553,
        "resultsPerPage": 1
    },
    "items": []
}

如您所见,items数组中没有视频,但它说有 553 totalResults。此外,如果您增加到maxResults2,您将获得 1 个视频。

标签: youtubeyoutube-data-apiyoutube-iframe-api

解决方案


在测试了您在问题中提供的示例请求后,我确实进行了以下修改:

  • 将该maxResults值增加到 2 或更大。对于这种情况,我将值设置为 5。根据文档,值的范围在 0 到 50 之间。

这是您提供的Google API Explorer 示例videoId- 在那里,我设置了带来特定字段的请求。

进行修改后,结果如下:

{
 "pageInfo": {
  "totalResults": 556,
  "resultsPerPage": 5
 },
 "items": [
  {
   "id": {
    "kind": "youtube#video",
    "videoId": "WlosjSe5B8c"
   },
   "snippet": {
    "channelId": "UCtylTUUVIGY_i5afsQYeBZA",
    "title": "Lil Skies - Red Roses ft. Landon Cube (Dir. by @_ColeBennett_)",
    "channelTitle": "Lyrical Lemonade"
   }
  },
  {
   "id": {
    "kind": "youtube#video",
    "videoId": "4eS5o3beHko"
   },
   "snippet": {
    "channelId": "UCtylTUUVIGY_i5afsQYeBZA",
    "title": "Lil Skies - Creeping ft. Rich The Kid (Dir. by @_ColeBennett_)",
    "channelTitle": "Lyrical Lemonade"
   }
  },
  {
   "id": {
    "kind": "youtube#video",
    "videoId": "mzB1VGEGcSU"
   },
   "snippet": {
    "channelId": "UCtylTUUVIGY_i5afsQYeBZA",
    "title": "Juice WRLD - Lucid Dreams (Dir. by @_ColeBennett_)",
    "channelTitle": "Lyrical Lemonade"
   }
  },
  {
   "id": {
    "kind": "youtube#video",
    "videoId": "kxloC1MKTpg"
   },
   "snippet": {
    "channelId": "UCrFB54bqp8sda4udJyNswlA",
    "title": "DJ Khaled - No Brainer (Official Video) ft. Justin Bieber, Chance the Rapper, Quavo",
    "channelTitle": "DJKhaledVEVO"
   }
  }
 ]
}

尽管响应显示pageInfo.totalResults为 556,但 YouTube 有自己的阈值,因此,我不相信在同一个响应中获得这些数据量。我相信的限制设置为 50。


推荐阅读