YouTube API v3 - List uploaded videos - youtube-api

How do I list the user's uploaded videos in the V3 api?

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
id={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

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({
id: playlistId,
part: 'snippet',
});
request.execute(function(response) {
// Go through response.result.playlistItems to view list of uploaded videos.
});

Related

YouTube Data API video duration is 1 second longer

For example, the HTTP request GET https://www.googleapis.com/youtube/v3/videos?id=kjDwZNs6GLs&part=contentDetails&key=[API_KEY] gives back the below response payload:
{
"kind": "youtube#videoListResponse",
"etag": "fMWPUAE-QPUqczFLIOqPLkA_HBs",
"items": [
{
"kind": "youtube#video",
"etag": "JVU1XymxBIBUDHOdlmlwJ1nlhTM",
"id": "4ZkEob55qso",
"contentDetails": {
"duration": "PT2M4S",
"dimension": "2d",
"definition": "sd",
"caption": "false",
"licensedContent": true,
"contentRating": {},
"projection": "rectangular"
}
}
],
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
}
}
The duration in the JSON payload is PT2M4S, but if you go check the actual video on YouTube https://www.youtube.com/watch?v=4ZkEob55qso the duration displayed in the video player is 2:03. I couldn't find any mention of this discrepancy in the API documentation https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration, does anyone know exactly why the YT Data API gives back a 1-second longer video duration?

YouTube API 3 channels by username and id inconsistant

Why is it when I search for a channel by ID https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=UU-tdS40X5ld-a4KfarLJuDw&key=[YOUR_API_KEY] I get no items returned.
But when I search by userName, it returns the channel ID that I just searched for https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=washingtonstateuniv&key=[YOUR_API_KEY]
when I search for a channel by ID .. I get no items returned.
Give the Try-it section for channels.list a try. I'm able to fetch JSON response by using these parameters:
part->contentDetails
id-> yourchannel ID
Authorize and Execute
The response it returned is not empty at all.
{
"kind": "youtube#channelListResponse",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/FiuYcDu7WFmoFVcLDRvENYGl_tQ\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/QjMybH99xT3x-znrh2Yerx_0DWk\"",
"id": "{my channel id}",
"snippet": {
"title": "{my channel name}",
"description": "",
"publishedAt": "2011-10-10T15:30:40.000Z",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/-EN5H1HDHqIU/ABCDEFGHIJK/ABCDEFGHIJK/l2gqeYg94P8/s88-c-k-no-mo-rj-c0xffffff/photo.jpg"
},
"medium": {
"url": "https://yt3.ggpht.com/-EN5H1HDHqIU/ABCDEFGHIJK/ABCDEFGHIJK/l2gqeYg94P8/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
},
"high": {
"url": "https://yt3.ggpht.com/-EN5H1HDHqIU/ABCDEFGHIJK/ABCDEFGHIJK/l2gqeYg94P8/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
}
},
"localized": {
"title": "{my channel name}",
"description": ""
}
}
}
]
}
That's a lot of results for me.
But when I search by userName, it returns the channel ID that I just
searched for.
In the channels.list try-it docs above, forUsername is defined as the forUsername parameter specifies a YouTube username, thereby requesting the channel associated with that username. So I think it's doing it's job.
Additional note on forUsername parameter is that's it's used to translate your arbitrary legacy YouTube username, that's the old Youtube accounts, into a channel ID using v3 of the API. More of that in Work with Channel IDs guide.
when I search by userName, it returns the channel ID that I just
searched for
You miss the important part to search by forUsername or channelID. That is snippet part, not contentDetails.
by forUsername :
https://www.googleapis.com/youtube/v3/channels?&part=snippet,id&forUsername=RealMiBs&title&key={YOUR_API_KEY}
by id channel:
https://www.googleapis.com/youtube/v3/channels?&part=snippet,id&id=UC_pwIXKXNm5KGhdEVzmY60A&title&key={YOUR_API_KEY}
That will exactly returned to like below:
{
"kind": "youtube#channelListResponse",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/VnicD0AYsCI7KlKKcdsmdIlWUMs\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/Nl4tuuOFwuPOdHmP_Ky3vuIQ2Gg\"",
"id": "UC_pwIXKXNm5KGhdEVzmY60A",
"snippet": {
"title": "CJENMMUSIC Official", <== Channel Name
"description": "Asia's No.1 Entertainment & Media Company", <== Channel Desctiption
"customUrl": "cjenmmusic", <=== Channel URL who has been qualified - to and for - claimed it
"publishedAt": "2011-03-25T04:48:40.000Z",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/-QMkGdFbhrOc/AAAAAAAAAAI/AAAAAAAAAAA/6boUKax-3EA/s88-c-k-no-mo-rj-c0xffffff/photo.jpg"
},
"medium": {
"url": "https://yt3.ggpht.com/-QMkGdFbhrOc/AAAAAAAAAAI/AAAAAAAAAAA/6boUKax-3EA/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
},
"high": {
"url": "https://yt3.ggpht.com/-QMkGdFbhrOc/AAAAAAAAAAI/AAAAAAAAAAA/6boUKax-3EA/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
}
},
"localized": {
"title": "CJENMMUSIC Official",
"description": "Asia's No.1 Entertainment & Media Company"
}
}
}
]
}
While if you just pointing to contentDetails, it will return to almost nothing:
{
"kind": "youtube#channelListResponse",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/C7SnOhT2c-Fs2R9f6JlxlOPWc34\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/3RTKaEQC9GX8c58R2Bhd8G1y3zM\"",
"id": "UC_pwIXKXNm5KGhdEVzmY60A",
"contentDetails": { <============== detail contents
"relatedPlaylists": {
"uploads": "UU_pwIXKXNm5KGhdEVzmY60A",
"watchHistory": "HL",
"watchLater": "WL"
}
}
}
]
}

Youtube API 3.0 search videos and get video statistics at single request

I'm searching youtube videos with youtube api 3.0.
I'm using this type of example API request
https://www.googleapis.com/youtube/v3/search?part=snippet&key=[API_KEY]
But i want to get statistics of videos with in the same api request. How to solve this problem.
Note: When I'm using statistics key with part. I got error.
I'm also tried this request
https://www.googleapis.com/youtube/v3/search?part=snippet,statistics&key=[API_KEY]
The resource search.list don't have the part statistics.
Step 1 :
You need to get the videoId of the video: "videoId": "UHdgXkkVyl0" with search.list.
The request :
https://www.googleapis.com/youtube/v3/search?part=id&q=tuto&type=video&key={YOUR_API_KEY}
The response:
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"MmqJLb8ZBOWRQIsg7xej7lrKLMI/34CzOO9FXYQg7kdlOeoe59LsWVk\"",
"id": {
"kind": "youtube#video",
"videoId": "UHdgXkkVyl0"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"MmqJLb8ZBOWRQIsg7xej7lrKLMI/U303dB0TgZ89ODlqdwuKs5efOdk\"",
"id": {
"kind": "youtube#video",
"videoId": "LvEA2KHWQec"
}
},
Step 2 :
After you have searched videos with search.list you need to make a second call to the API with the resource video.list with parameters :
part: statistics
id: "id of the video found in previous request"
If you have more than one video id you can specify the id of videos with a comma-separated list like :
id: "Xxsdw6zG1bg, Xxsdw6zG1bg,...." )
The request: https://www.googleapis.com/youtube/v3/videos?part=statistics&id=UHdgXkkVyl0%2C+Xxsdw6zG1bg&key={YOUR_API_KEY}
The response will be like this :
{
"kind": "youtube#videoListResponse",
"etag": "\"MmqJLb8ZBOWRQIsg7xej7lrKLMI/rxvjZzq2nNqBg7Me5VQv1ToZm64\"",
"pageInfo": {
"totalResults": 2,
"resultsPerPage": 2
},
"items": [
{
"kind": "youtube#video",
"etag": "\"MmqJLb8ZBOWRQIsg7xej7lrKLMI/3fah-cngFxFOnytseMYZU1TK_-8\"",
"id": "UHdgXkkVyl0",
"statistics": {
"viewCount": "3070836",
"likeCount": "72140",
"dislikeCount": "1132",
"favoriteCount": "0",
"commentCount": "7798"
}
},
{
"kind": "youtube#video",
"etag": "\"MmqJLb8ZBOWRQIsg7xej7lrKLMI/J4xM7Dd23TGYU6on-PESyEIAE9A\"",
"id": "Xxsdw6zG1bg",
"statistics": {
"viewCount": "131487",
"likeCount": "1459",
"dislikeCount": "25",
"favoriteCount": "0",
"commentCount": "39"
}
}
]
}
And you have the statistics !
Unfortunately, dislikeCount attribute has became private, in december 2021. according the youtube api docs.

How can I get a list of videos from a playlist in an auto-generated YouTube channel via the API?

YouTube has introduced auto-generated channels, for example http://www.youtube.com/channel/UCH-BBvNWh1CONPAjpeocGcw. From the channelId, I can use the API
to retrieve its associated playlists:
GET https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&channelId=UCH-BBvNWh1CONPAjpeocGcw&key={YOUR_API_KEY}
gets, in part:
"items": [
{
"kind": "youtube#playlist",
"etag": "\"GbgM9_0DKhSLzW6BxAmfOJZH9RI/q_Rxfox9sHfH_r9g_LimnQeVsyU\"",
"id": "ALNb4maWNoT6RXC29sfG8iREznyAb9tqqJ",
"contentDetails": {
"itemCount": 95
}
},......
Each playlist has an id and an itemCount. When I use the playlistId, I get no items back:
GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&id=ALNb4maWNoT6RXC29sfG8iREznyAb9tqqJ&key={YOUR_API_KEY}
gets me
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"GbgM9_0DKhSLzW6BxAmfOJZH9RI/3cxjRXf86G9z5Bg7rup3QfCgrxM\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": [
]
}
Am I missing a step?
Solved my own problem. The playlistId should be the "playlistId" parameter, not "id".
GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=ALNb4maWNoT6RXC29sfG8iREznyAb9tqqJ&key={YOUR_API_KEY}

How to Get All Movies (not videos) with YouTube V3 API

I want to list all the movies available on YouTube (i.e. those available via //youtube.com/movies). I can see how to query those using API V2, but this has a limit of 1000 responses.
With the V3 api I can see the category for movies has id 30, but I can't work out how to get all movies with this category. The only category queries appear to search on guide categories, which are different.
Any clues on how I can get this list? Please help if you can!
I believe that you can get the autogenerated channels, related to movies, and use their id to get the playlists related to this channels and finally the items (videos..) from this playlists, that must be the videos related to the movies of the selected channels:
Retrieve movie channels:
Request
https://www.googleapis.com/youtube/v3/search?part=snippet&q=movies&type=channel&key={YOUR_API_KEY}
Partial response
"items": [
{
"id": {
"kind": "youtube#channel",
"channelId": "UCczhp4wznQWonO7Pb8HQ2MQ"
},
"kind": "youtube#searchResult",
"etag": "\"eTr3dHIt5_K9qdGtRKL-5XdpiQI/sQpwXP-0MUEZzOQx4F0yKj0eUR4\"",
"snippet": {
"publishedAt": "2005-12-15T03:07:36.000Z",
"channelId": "UCczhp4wznQWonO7Pb8HQ2MQ",
"title": "movies",
"description": "YouTube Movies (United States).",
"thumbnails": {
"default": {
"url": "http://i.ytimg.com/i/czhp4wznQWonO7Pb8HQ2MQ/1.jpg"
},
"medium": {
"url": "http://i.ytimg.com/i/czhp4wznQWonO7Pb8HQ2MQ/mq1.jpg"
},
"high": {
"url": "http://i.ytimg.com/i/czhp4wznQWonO7Pb8HQ2MQ/hq1.jpg"
}
}
}
},
e.g. Get the list of movies in channel "YouTube Movies (United States)"
Request
https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCczhp4wznQWonO7Pb8HQ2MQ&key={YOUR_API_KEY}
Partial response:
{
"id": "PLjygWhZE6KY0Uhw12FsAc8raAClz0l71C",
"kind": "youtube#playlist",
"etag": "\"eTr3dHIt5_K9qdGtRKL-5XdpiQI/TWrkoCkmJvq14neCcutnApHMMgU\"",
"snippet": {
"publishedAt": "2012-12-06T20:11:40.000Z",
"channelId": "UCczhp4wznQWonO7Pb8HQ2MQ",
"title": "The Nicolas Cage Collection",
"description": "From panicked fathers to spirits of vengeance, Nicolas Cage has done it all.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/nHiy8SVZdpU/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/nHiy8SVZdpU/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/nHiy8SVZdpU/hqdefault.jpg"
}
}
}
},
Get items from this playlist:
Request:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PLjygWhZE6KY0Uhw12FsAc8raAClz0l71C&key={YOUR_API_KEY}
Partial response:
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"eTr3dHIt5_K9qdGtRKL-5XdpiQI/KSlqEnNGq36l47_k9W3fd79KfYQ\"",
"pageInfo": {
"totalResults": 15,
"resultsPerPage": 5
},
"nextPageToken": "CAUQAA",
"items": [
{
"id": "PLcBtbpFAOApiV34TU797yEFAUZuwhFrvJya3MqlZWkGM",
"kind": "youtube#playlistItem",
"etag": "\"eTr3dHIt5_K9qdGtRKL-5XdpiQI/C_xTa48Xof7giXobSula2vWX43A\"",
"snippet": {
"publishedAt": "2012-12-06T20:12:49.000Z",
"channelId": "UCczhp4wznQWonO7Pb8HQ2MQ",
"title": "Stolen",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/nHiy8SVZdpU/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/nHiy8SVZdpU/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/nHiy8SVZdpU/hqdefault.jpg"
}
},
"playlistId": "PLjygWhZE6KY0Uhw12FsAc8raAClz0l71C",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "nHiy8SVZdpU"
}
}
},
{
"id": "PLcBtbpFAOApiV34TU797yED-1zeMCoMGVxtVgtX9ZdsQ",
"kind": "youtube#playlistItem",
"etag": "\"eTr3dHIt5_K9qdGtRKL-5XdpiQI/LgSyqtZkwJXv6-2ajW0g-FsgddQ\"",
"snippet": {
"publishedAt": "2012-12-06T20:19:26.000Z",
"channelId": "UCczhp4wznQWonO7Pb8HQ2MQ",
"title": "Trespass",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/J_7Zug6ouy4/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/J_7Zug6ouy4/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/J_7Zug6ouy4/hqdefault.jpg"
}
},
"playlistId": "PLjygWhZE6KY0Uhw12FsAc8raAClz0l71C",
"position": 1,
"resourceId": {
"kind": "youtube#video",
"videoId": "J_7Zug6ouy4"
}
}
},
and in this way you can create the list of videos as you want
Here's my current answer - still working on it so I've not accepted it yet
I worked out the categoryId for Movies is 30. I then did a search for videos using this categoryId, and the query string "movie" (just because I need to put something). This gives more than 1000 results, and google limits queries to 1000 results even with the v3 api.
So I need to partition my query. I have been able to do this with publishedBefore and publishedAfter. Note these are upload dates and not release dates.
This gives me a list of movies.
Here's a sample query
https://www.googleapis.com/youtube/v3/search?nextPageToken=CDIQAA&publishedBefore=2010-02-01T00%3A00%3A00Z&maxResults=50&q=movie&videoCategoryId=30&part=id%2Csnippet&key=yourAPIKeyHere&type=video&publishedAfter=2010-01-01T00%3A00%3A00Z
Once I have the movie IDs I query the individual movie ids
https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails&maxResults=50&id=O7cSKNevYQc%2CN84-SoboM6c%2C9K_WO3lqDSc%2CWTJP0Z-bknc%2CdF9hwjeRbGE%2CPnefvJ9T1N0%2CDuM6B4yKgTU%2C0tI8iP2f58I%2Cr4lNJEnY0FA%2COFnMrx6yEcA%2CmUZUAeqOmbg%2CDjq0tHK-KTo%2CkY44nxk0qjg%2CcLYkUR5MY14%2CTZpcfYOOEXA%2C-0jA9DMX8Qs%2C2TQDV1m4X2s%2CBh3Tye1OQvk%2Ct5IwjMDVNz4%2CkOi88X6xeKg%2Ck53CuaAUtik%2CNaRuenqLb9g%2Cn9h-0Wgix7Q%2CQG8SiW2a_l0%2Cmk-D66Z1Ydg%2ClvmwofckpNc%2CgRQK4fTXfBM%2CPZHgLy48R3Q%2CwczeO0DVM0g%2CTpMDTG3dEYE%2C6JI8pN7BqEQ%2COv9yllk3hsY%2CsN09sfLPu0g%2CbfhLYJGN948%2CPiWusdK75Ys%2CeE5jh0YwTCY%2C_cIw3vr2Q18%2CSA4xg_k0aqI%2CUZdha0zTM6w%2CwUdkDSIBw94%2CAhR-LDSIOaI%2C1XMt40vJayU%2C83fPx5-aUL8%2CmJLjaKzu7PQ%2CZvj_zRGnwU0%2CtyIPMd7JXOA%2CToE10sC36KQ%2COVKXjOW6cD8%2CLPW1cXVjMrg&key=LALALA
I still then need to filter the results by looking at the following json in the results
"contentDetails": {
"duration": "PT1H35M14S",
"regionRestriction": {
"allowed": [
"US"
]
}
Unfortunately this still doesn't give me the movie year or director (which are useful for working out which movie this actually is), or the price (which, you know, people find interesting)

Resources