How do I get a list of uploaded videos for a certain channel with the new YouTube Data API (V3)? - youtube-api

I am trying to get a list of video ids for all uploaded videos to a channel. I would also like to use the new version of the YouTube Data API (V3). How do I do this?

You have to get the upload playlist id to get each videos uploaded. To get that, you need to get the channel id. After you have the playlist id from the channel id, it is pretty simple. I have written out the steps for all three below.
Also, we offer PubSubHubBub which allows you to be alerted every time a new video is added to a channel, or you could use SUP (V2) to see which resources have changed before making the calls.
Instructions to get video ids for all uploaded videos for a channel in V3
Get the channel id for the channel you want (you probably only need to do this once, then you can save it)
Use search.list
Set type to channel
Set q to the name of the channel you want
Grab the channel id (something like this: "channelId": "UC0X2VuXXXXXXXXXXXXXXXX")
Get the playlist id for the channel uploads using the channel id from step 1 (you probably only need to do this once, then you can save it)
Use channels.list
Set id to UC0X2VuXXXXXXXXXXXXXXXX from step 1
Grab the uploads key from contentDetails (something like this: "uploads": "UU0XXXXXXXXXXXXXXXXXXXX")
Get the videos via the playlistitems in the playlist using the playlist id from step 2
Use playlistItems.list
Set playlistId to UU0XXXXXXXXXXXXXXXXXXXX from step 2
Go through each PlaylistItem and pull out the video id

In the meantime, there is a much easier way:
Use the channels.list
set forUsername = [CHANNELNAME]
set part = contentDetails
grab $data->items[0]->contentDetails->relatedPlaylists->uploads --> [PLAYLISTID]
Use playlistItems.list
Set playlistId = [PLAYLISTID] from step 1
Go through each PlaylistItem and pull out the video ids

Related

YouTube Api v3: calculate youtube playlist length

How can i get a youtube playlist duration (the sum of all it's videos durations).
I used this endpoint to get the playlist videos :
https://youtube.googleapis.com/youtube/v3/videos?playlistId
and it's return the videos object with all videos ids, in this case i need to make an api call to get each video length with separate api call to :
https://youtube.googleapis.com/youtube/v3/videos
I searched for a solution for this and found that we can make a call to previous api with all ids each id with a query param ex. id=aaaaa&id=bbbbb, but i need to be the owner of these videos to make the request.
Is there any way to get all videos with one call and without being the owner of the videos ?

How list all videos from a YouTube channel?

https://www.googleapis.com/youtube/v3/channels?id=UC_x5XG1OV2P6uZZ5FSM9Ttw&part=snippet%2CcontentDetails%2Cstatistics&key={YOUR_API_KEY}
Using this API link, I get a list of videos from start but not all videos. I only get 300 to 500 videos.
This is a bit complicated. You have to make a few calls instead.
find the channel ID of the channel you want.
list playlists (youtube.channels.list set id to channelId and set part to contentDetails)
find the ID of the playlist with name uploads
list playlist items (youtube.playlistItems.list set playlistId and set part to snippet optionally set maxResults to 50)
Page through results using nextPageToken
Found this from here https://stackoverflow.com/a/27872244/975887
Hope this helps.

How can I query YouTube API to get videos from auto-generated 'Topic' channels?

How can I input search parameters using the YouTube API so that I only get video ID's from topic channels? For instance, if I go to youtube.com and manually search "Hello Adele - Topic" I correctly get the song, 'Hello' from the 'Adele - Topic' channel as my first search result, https://www.youtube.com/watch?v=_WS9w10ygpU.
However, if I perform the same query using the YouTube Data API, with the parameters:
part='snippet'
q='Hello Adele - Topic'
I won't get back the ID for the correct video. Is there any way to get this programmatically?
As stated in this thread, auto generated channels have no videos. They have only playlists with videos from other channels. So you have to look for playlists. You may check the sample request in this link.
Here's the URL sample of auto-generated Topic-based Id which grabs its playlist id:
GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=HC9m3exs6zk1U&fields=items%2Fid&key={YOUR_API_KEY}
//Outputs sample playlist Id: LP9m3exs6zk1U
Now here's the URL sample using that playlist Id to get the videos from the auto-generated Topic-based channel Id:
GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=5&playlistId=LP9m3exs6zk1U&key={YOUR_API_KEY}
//Outputs video data you want.
Hope this helps.

Get all videos for a given user

I'm trying to find a way to retrieve all videos posted by a given user (eg: https://www.youtube.com/user/laliga/videos).
All the examples I've found online or the doc (https://developers.google.com/youtube/v3/docs/) seems to require either a playlist ID, channel ID or video ID.
Is there any way I can directly query the author's videos or am I forced to retrieve all playlists and then iterate over?
Thanks
You just need to map the user name to its channelID first via the YouTube API like so:
https://www.googleapis.com/youtube/v3/channels?forUsername=USER_NAME&part=id&key=API_KEY
Full docs: https://developers.google.com/youtube/v3/docs/channels/list
Then you can make a Videos:List call with that channelID to get the videos.
1)take your google key id and channel id and put inside below url
https://www.googleapis.com/youtube/v3/search?key=APIKEY&channelId=CHANNELID&part=snippet,id&order=date&maxResults=20
ex
https://www.googleapis.com/youtube/v3/search?key=adsjsdakhkjshd&channelId=sdahghsadhgj&part=snippet,id&order=date&maxResults=20

Retrieve user by channel ID?

I am retrieving all of the videos for a given channel by:
'https://gdata.youtube.com/feeds/api/users/{username}/uploads'
When I go to a channel, such as: http://www.youtube.com/channel/UCXIyz409s7bNWVcM-vjfdVA I have the channel ID and I can quite clearly see the videos. Also, if I click on the 'Videos' tab I can see all the videos, but the URL changes to: http://www.youtube.com/user/majesticcasual/videos
I would like to take the channel ID and retrieve the username for the given channel such that I can query YouTube's API for the videos by channel ID. Is this possible?
If you retrieve this feed:
https://gdata.youtube.com/feeds/api/users/[channel_id]/
You'll get a response that includes an <author> element -- the <name> child of that <author> element is the username. It's also repeated in that same feed as <yt:username>.
Of course, that's kind of moot, because you can query by channelID directly:
https://gdata.youtube.com/feeds/api/users/[channel_id]/uploads
is the same feed as if you'd used the username. This works because the channel_id really is just the unique ID of someone's username.
If you want to use v3 of the API (Which is highly encouraged, as it's at production level now), you could use this feed:
GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUXIyz409s7bNWVcM-vjfdVA&key={YOUR_API_KEY}
Note that I've changed the channel id so instead of starting with UC (as all channel ids do), I'm passing in a value that starts with UU ... this is so you get back the uploads feed of the channel (you could also have it start with LL instead to get back the 'likes' feed, for example ... or even do a request to:
GET https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails&id=UCXIyz409s7bNWVcM-vjfdVA&key={YOUR_API_KEY}
To retrieve, in the contentDetails parameter, all of the playlists associated with that particular channel.

Resources