检索播放列表中包含的视频ID-YouTube API v3

检索播放列表中包含的视频ID-YouTube API v3,youtube,youtube-api,playlist,youtube-data-api,Youtube,Youtube Api,Playlist,Youtube Data Api,我一直在使用以下方法检索特定YouTube播放列表中包含的所有视频ID、标题和默认缩略图图像,以便在动态播放列表转盘中使用(然后将ID传递给一个数组,该数组用于动态创建缩略图转盘。注意,“playlist”变量是通过外部化的XML值定义的): YouTube已经对v1-2 API进行了删减,并且该方法的工作时间更长。YouTube API v3是否提供了等效的方法?具体替换调用: var playListURL = 'http://gdata.youtube.com/feeds/api/play

我一直在使用以下方法检索特定YouTube播放列表中包含的所有视频ID、标题和默认缩略图图像,以便在动态播放列表转盘中使用(然后将ID传递给一个数组,该数组用于动态创建缩略图转盘。注意,“playlist”变量是通过外部化的XML值定义的):

YouTube已经对v1-2 API进行了删减,并且该方法的工作时间更长。YouTube API v3是否提供了等效的方法?具体替换调用:

var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/' + playlist + '?v=2&alt=json&callback=?';

您需要切换到新的端点才能从播放列表中获取视频信息

请求:

HTTP GET: GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2C+id&playlistId=PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk&key={YOUR_API_KEY}
响应(对于id=PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk):


items
数组中每个对象的
id
值应该是您想要的
videoId
值。

事实上,看起来id不是视频id:/n您不能只获取播放列表项id并像
https://www.youtube.com/watch?v={{video_id}
事实上,您可以在
item.index.contentDetails.videoId
节点中找到您的id。如何获取包含播放列表id和索引号的视频详细信息?参加聚会时迟到,遇到同样的问题:items.id表示播放列表项的id,而不是视频(播放列表项是嵌入视频的容器)。对于视频id,只需使用items.snippet.resourceid.videoid这个答案教会了我如何使用javascript从YouTube播放列表中获取所有视频id:
api_key = 'YOUR_API_KEY'
api_service_name = 'youtube'
api_version = 'v3'
playlist_id = 'SOME_PLAYLIST_ID'

# build a connected client
youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey=api_key)

# make a request (note, only the 'snippet' part has been requested)
request = youtube.playlistItems().list(part=['snippet'], playlistId=playlist_id)
response = request.execute()

# scrape out the actual video ids (probably could be safer)
for (k, v) in response.items():
    if(k == 'items'):
        video_ids = [pli['snippet']['resourceId']['videoId'] for pli in v if pli['snippet']['resourceId']['kind']=='youtube#video']

print(video_ids)
{
 "kind": "youtube#playlistItemListResponse",
 "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/pBQT3k1NWD9D6HOvzNhT4QS1-W0\"",
 "nextPageToken": "CAUQAA",
 "pageInfo": {
  "totalResults": 10,
  "resultsPerPage": 5
 },
 "items": [
  {

   "kind": "youtube#playlistItem",
   "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/wu_RMwA6QYinWSMrYXyOugKDbyI\"",
   "id": "PLl3eZeHB033DOxfNMOVENodvC_4QtQ-r2bxEjeNprDxQ",
   "snippet": {
    "publishedAt": "2014-12-10T21:16:39.000Z",
    "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
    "title": "Private video",
    "description": "This video is private.",
    "channelTitle": "Late Show with David Letterman",
    "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
    "position": 0,
    "resourceId": {
     "kind": "youtube#video",
     "videoId": "4bVgilYncao"
    }
   }
  },
  {

   "kind": "youtube#playlistItem",
   "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/DPNyicQckvtlwgMTintYEwYsE6g\"",
   "id": "PLl3eZeHB033DOxfNMOVENoaZ-Ybk0W0zkTL1Sliq5xeA",
   "snippet": {
    "publishedAt": "2014-12-10T21:16:58.000Z",
    "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
    "title": "David Letterman Announces His Retirement from the Late Show",
    "description": "David Letterman announces that he will retire from the Late Show in 2015.\n\nLive on Letterman is up for a Webby award! Let your voice be heard and vote now! http://bit.ly/1l4oktE #WebbyAwards",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/hqdefault.jpg",
      "width": 480,
      "height": 360
     },
     "standard": {
      "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/sddefault.jpg",
      "width": 640,
      "height": 480
     },
     "maxres": {
      "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/maxresdefault.jpg",
      "width": 1280,
      "height": 720
     }
    },
    "channelTitle": "Late Show with David Letterman",
    "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
    "position": 1,
    "resourceId": {
     "kind": "youtube#video",
     "videoId": "H9eYkpgeeI8"
    }
   }
  },
  {

   "kind": "youtube#playlistItem",
   "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/aL6XWPV0PoM50wQcyclAxsvtWy4\"",
   "id": "PLl3eZeHB033DOxfNMOVENobu4IxNcwxZw1eEEoLqHjhY",
   "snippet": {
    "publishedAt": "2014-12-10T21:17:14.000Z",
    "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
    "title": "Private video",
    "description": "This video is private.",
    "channelTitle": "Late Show with David Letterman",
    "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
    "position": 2,
    "resourceId": {
     "kind": "youtube#video",
     "videoId": "ONgN2Hgz3XE"
    }
   }
  },
  {

   "kind": "youtube#playlistItem",
   "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/YF1JYH8bTG20vjuPLeMR-2xkxjo\"",
   "id": "PLl3eZeHB033DOxfNMOVENoQncF7sXo86YALoy5U4yqlU",
   "snippet": {
    "publishedAt": "2014-12-10T21:17:30.000Z",
    "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
    "title": "David Letterman - Future Islands: \"Seasons (Waiting On You)\"",
    "description": "Making their network TV debut, Future Islands performed \"Seasons (Waiting On You)\" from their album, \"Singles.\"",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Late Show with David Letterman",
    "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
    "position": 3,
    "resourceId": {
     "kind": "youtube#video",
     "videoId": "1Ee4bfu_t3c"
    }
   }
  },
  {

   "kind": "youtube#playlistItem",
   "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/_Ft_7ujRwQbP7-altelmVnzwQx0\"",
   "id": "PLl3eZeHB033DOxfNMOVENoYQ9Z55P4r2KhvkV5yx-okc",
   "snippet": {
    "publishedAt": "2014-12-10T21:17:47.000Z",
    "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
    "title": "Private video",
    "description": "This video is private.",
    "channelTitle": "Late Show with David Letterman",
    "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
    "position": 4,
    "resourceId": {
     "kind": "youtube#video",
     "videoId": "Bzr5VtFvSyw"
    }
   }
  }
 ]
}
api_key = 'YOUR_API_KEY'
api_service_name = 'youtube'
api_version = 'v3'
playlist_id = 'SOME_PLAYLIST_ID'

# build a connected client
youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey=api_key)

# make a request (note, only the 'snippet' part has been requested)
request = youtube.playlistItems().list(part=['snippet'], playlistId=playlist_id)
response = request.execute()

# scrape out the actual video ids (probably could be safer)
for (k, v) in response.items():
    if(k == 'items'):
        video_ids = [pli['snippet']['resourceId']['videoId'] for pli in v if pli['snippet']['resourceId']['kind']=='youtube#video']

print(video_ids)