如何使用 YouTube API 从 YouTube 频道获取所有视频 ID


How can I get all Video IDs form a YouTube Channel by using YouTube API?

有没有办法从YouTube频道获取所有视频ID?

YouTube API v2 去年被弃用。花一些时间研究 v3,...它有很大的不同,因为您需要进行一系列调用才能获得在 v2 中得到的相同内容,并且您的旧代码都不起作用,但在其他方面具有更好的控制,...只是很糟糕的健谈而已。

让我们以一个示例频道及其示例视频为例。

现在我们必须从video_id URL https://www.youtube.com/watch?v=PfrV_6yWbEgwe 中获取channel_id

,这是PfrV_6yWbEgwe

要使用video_id获取channel_id,您可以使用YouTube数据API的videos:list端点 - 示例。

结果:

{
  "kind": "youtube#videoListResponse",
  "etag": "qh1OskO-aIVgApaf_ko2NP56aTw",
  "items": [
    {
      "kind": "youtube#video",
      "etag": "eIO610C5Nr92qVJbX_pQYb9kc0Y",
      "id": "PfrV_6yWbEg",
      "snippet": {
        "publishedAt": "2016-10-28T22:48:23Z",
        "channelId": "UCulYu1HEIa7f70L2lYZWHOw",
        "title": "Michael Jackson - Who Is It (Official Video)",
        "description": "“Who Is It” by Michael Jackson'nListen to Michael Jackson: https://MichaelJackson.lnk.to/_listenYD'n'nMichael Jackson’s “Who Is It” short film, directed by David Fincher, spins a dark, expressionistic tale of heartbreak over a lover’s infidelity that complements this worldwide Top 10 hit song.'n'nFollow Michael Jackson:'nFacebook: https://MichaelJackson.lnk.to/followFI'nTwitter: https://MichaelJackson.lnk.to/followTI'nInstagram: https://MichaelJackson.lnk.to/followII'nWebsite: https://MichaelJackson.lnk.to/followWI'nSpotify: https://MichaelJackson.lnk.to/followSI'nYouTube: https://MichaelJackson.lnk.to/subscribeYD'n'nChorus:'nIs it a friend of mine'n(Who is it?)'nIs it my brother?'n(Who is it?)'nSomebody hurt my soul'n(Who is it?)'nI can't take this stuff no more'n'n#MichaelJackson #Dangerous #WhoIsIt",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/PfrV_6yWbEg/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/PfrV_6yWbEg/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/PfrV_6yWbEg/hqdefault.jpg",
            "width": 480,
            "height": 360
          }
        },
        "channelTitle": "michaeljacksonVEVO",
        "tags": [
          "1991",
          "new jack swing",
          "Dangerous",
          "Teddy Riley",
          "fourth album",
          "ocean way studios",
          "record one studios",
          "michael jackson",
          "michael jackson Who Is It",
          "michael jackson dangerous live",
          "michael jackson dangerous album",
          "michael jackson dangerous tour",
          "michael jackson dangerous lyrics",
          "michael jackson songs",
          "beat it",
          "billie jean",
          "michael jackson pyt",
          "michael jackson thriller",
          "michael jackson black or white",
          "smooth criminal",
          "man in the mirror",
          "why",
          "you"
        ],
        "categoryId": "10",
        "liveBroadcastContent": "none",
        "localized": {
          "title": "Michael Jackson - Who Is It (Official Video)",
          "description": "“Who Is It” by Michael Jackson'nListen to Michael Jackson: https://MichaelJackson.lnk.to/_listenYD'n'nMichael Jackson’s “Who Is It” short film, directed by David Fincher, spins a dark, expressionistic tale of heartbreak over a lover’s infidelity that complements this worldwide Top 10 hit song.'n'nFollow Michael Jackson:'nFacebook: https://MichaelJackson.lnk.to/followFI'nTwitter: https://MichaelJackson.lnk.to/followTI'nInstagram: https://MichaelJackson.lnk.to/followII'nWebsite: https://MichaelJackson.lnk.to/followWI'nSpotify: https://MichaelJackson.lnk.to/followSI'nYouTube: https://MichaelJackson.lnk.to/subscribeYD'n'nChorus:'nIs it a friend of mine'n(Who is it?)'nIs it my brother?'n(Who is it?)'nSomebody hurt my soul'n(Who is it?)'nI can't take this stuff no more'n'n#MichaelJackson #Dangerous #WhoIsIt"
        },
        "defaultAudioLanguage": "en-US"
      }
    }
  ],
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  }
}

在这种情况下,示例视频PfrV_6yWbEgchannel_id为:UCulYu1HEIa7f70L2lYZWHOw

然后,使用此channel_id,将第二个字符更改为U,如下所示:

  • 未修改: UCulYu1HEIa7f70L2lYZWHOw
  • 修改: UU ulYu1HEIa7f70L2lYZWHOw

此修改后的 id 是该 YouTube 频道的上传播放列表。

通过此上传playlist_id,您可以使用 YouTube 数据 API 的播放列表项:列表端点从自动生成的频道检索所有上传的视频。

这是本答案中描述的上传playlist_id示例。