34

如何在 V3 api 中列出用户上传的视频?

4

2 回答 2

41

如果您使用的是客户端,那么 Greg 的回答是正确的。要对基本请求执行相同的操作,您需要发出以下 2 个请求:

  1. 获取https://www.googleapis.com/youtube/v3/channels

    带参数:

    part=contentDetails
    mine=true
    key={YOUR_API_KEY}
    

    和标题:

    Authorization:  Bearer {Your access token}
    

    从这里你会得到一个像这样的 JSON 响应:

    {
     "kind": "youtube#channelListResponse",
     "etag": "\"some-string\"",
     "pageInfo": {
      "totalResults": 1,
      "resultsPerPage": 1
     },
     "items": [
      {
       "id": "some-id",
       "kind": "youtube#channel",
       "etag": "\"another-string\"",
       "contentDetails": {
        "relatedPlaylists": {
         "likes": "channel-id-for-your-likes",
         "favorites": "channel-id-for-your-favorites",
         "uploads": "channel-id-for-your-uploads",
         "watchHistory": "channel-id-for-your-watch-history",
         "watchLater": "channel-id-for-your-watch-later"
        }
       }
      }
     ]
    }
    

    从这里你想解析出“上传”频道ID。

  2. 获取https://www.googleapis.com/youtube/v3/playlistItems

    带参数:

    part=snippet
    maxResults=50
    playlistId={YOUR_UPLOAD_PLAYLIST_ID}
    key={YOUR_API_KEY}
    

    和标题:

    Authorization:  Bearer {YOUR_TOKEN}
    

    从这里您将收到如下 JSON 响应:

    {
     "kind": "youtube#playlistItemListResponse",
     "etag": "\"some-string\"",
     "pageInfo": {
      "totalResults": 1,
      "resultsPerPage": 50
     },
     "items": [
      {
    
       "id": "some-id",
       "kind": "youtube#playlistItem",
       "etag": "\"another-string\"",
       "snippet": {
        "publishedAt": "some-date",
        "channelId": "the-channel-id",
        "title": "video-title",
        "thumbnails": {
         "default": {
          "url": "thumbnail-address"
         },
         "medium": {
          "url": "thumbnail-address"
         },
         "high": {
          "url": "thumbnail-address"
         }
        },
        "playlistId": "upload-playlist-id",
        "position": 0,
        "resourceId": {
         "kind": "youtube#video",
         "videoId": "the-videos-id"
        }
       }
      }
     ]
    }
    

使用这种方法,您应该能够使用任何语言甚至 curl 获取信息。如果您想要超过前 50 个结果,则必须使用第二个请求执行多个查询并传入页面请求。可以在以下位置阅读更多信息:http: //developers.google.com/youtube/v3/docs/playlistItems/list

于 2012-11-26T17:20:39.407 回答
33

第一步是获取该用户的频道 ID。我们可以通过对服务的请求来做到这一点Channels。这是一个JS示例。

var request = gapi.client.youtube.channels.list({
  // mine: true indicates that we want to retrieve the channel for the authenticated user.
  mine: true,
  part: 'contentDetails'
});
request.execute(function(response) {
  playlistId = response.result.channels[0].contentDetails.uploads;
});

一旦我们获得播放列表 ID,我们就可以使用它来查询来自PlaylistItems服务的上传视频列表。

var request = gapi.client.youtube.playlistItems.list({
  playlistId: playlistId,
  part: 'snippet',
});
request.execute(function(response) {
  // Go through response.result.playlistItems to view list of uploaded videos.
});
于 2012-10-17T17:12:56.223 回答