如何在V3 api中列出用户上传的视频?
答案 0 :(得分:41)
如果您使用的是客户,那么Greg的回答是正确的。要对基本请求执行相同的操作,请执行以下2个请求:
获取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。
获取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"
}
}
}
]
}
使用此方法,您应该能够使用任何语言获取信息,甚至只是卷曲。如果您想要超过前50个结果,那么您将不得不使用第二个请求进行多个查询并传入页面请求。有关详细信息,请参阅:http://developers.google.com/youtube/v3/docs/playlistItems/list
答案 1 :(得分:31)
第一步是获取该用户的频道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.
});