问题
How do I list the user's uploaded videos in the V3 api?
回答1:
The first step is getting the channel id for that user. We can do this with request to the Channels
service. Here's a JS example.
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;
});
Once we get the playlist id we can use that to query for the list of uploaded videos from the PlaylistItems
service.
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.
});
回答2:
If you are using the client then Greg's answer is correct. To do the same thing with basic requests you make the following 2 requests:
GET https://www.googleapis.com/youtube/v3/channels
with parameters:
part=contentDetails mine=true key={YOUR_API_KEY}
and header:
Authorization: Bearer {Your access token}
From this you will get a JSON response like so:
{ "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" } } } ] }
From this you want to parse out the "uploads" channel-id.
GET https://www.googleapis.com/youtube/v3/playlistItems
with parameters:
part=snippet maxResults=50 playlistId={YOUR_UPLOAD_PLAYLIST_ID} key={YOUR_API_KEY}
and headers:
Authorization: Bearer {YOUR_TOKEN}
From this you will receive a JSON response like the following:
{ "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" } } } ] }
With this method you should be able to get the info using any language or even just curl. If you want more than the first 50 results, then you will have to do multiple queries using the second request and pass in page requests. More on this can be read at: http://developers.google.com/youtube/v3/docs/playlistItems/list
来源:https://stackoverflow.com/questions/12930200/youtube-api-v3-list-uploaded-videos