Reputation: 11
I want to automatically embed my recently uploaded video from my YouTube channel in the sidebar of my website.
I got a good answer but it include user id they asked for
youtube.com/user/SomeName
but in my case, it's like youtube.com/CHANNEL/name
. I tried replacing "SomeName" with my channel id, but it didn't work for me
Upvotes: 0
Views: 2036
Reputation: 11
Thanks everyone for your answer. I found a pretty easy way to do this All you need is id for your upload playlist and to find that visit google API explorer
Services > YouTube Data API v3 > youtube.channels.list
fill in the part section with "contentDetails" and id section with youtube channel id (the one starts with UC) scroll down and click on execute
you will find
"uploads": "UUIprTMT-TDysEDx03YMcS5Q",
in ** "relatedPlaylists"** section copy that tag and now use this code to embed
<div style="position:relative;height:0;padding-bottom:56.25%"><iframe src="https://www.youtube.com/embed/videoseries?list=YOUR_UPLOAD_TAG" width="640" height="360" frameborder="0" style="position:absolute;width:100%;height:100%;left:0" allowfullscreen></iframe></div>
And you are done :)
Upvotes: 1
Reputation: 785
Firstly, many options have been removed from the API from V2 to V3.
A few things still remain, but are not going to help unless you know that it will be used.
One is the user name. Legacy Usernames are still around for channels that are old enough, but newer ones will not have them.
This is normally the Title
of the page, but the title can now be changed.
This is where the ID's come into it. Everyone has a unique ID, but can have the same Tile as another page. (even if confusing some times)
Example of an old channel is Google
https://www.youtube.com/user/Google
The "user" name is a Legacy name.
But all channels old and new will have a "Channel" as well
https://www.youtube.com/channel/UCK8sQmJBp8GCxrOtXWBpyEA
Note that it will give you an ID rather than a name.
So to get you what you are looking for, will need to be thought through.
Will the channel have a legacy user name? Do you need to show a name? or do you only want to find the last uploaded video regardless of name?
Best practice today will be to use the ID. Then with the API, get the relevant information that you need.
Step one is to call
https://www.googleapis.com/youtube/v3/channels?part=contentDetails,id,snippet&id=[THE_CHANNEL_ID]&key=[apiKey]
The contentDetails
section will contain an uploads
portion.
This will be the user uploads to the channel and will start with UU
Though a short cut will be to take the channel ID starting with UC and change it to UU. But I would recommend the API approach for security and peace of mind.
Then with the uploads playlist ID (the one starting with UU), you can call
https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails&maxResults=1&playlistId=[PLAYLIST_ID]&key=[api-key]
Normally the first in the array will be the last one in. The newest one. So setting results
to 1
will give only the first in the array.
If you want to do further checks to make sure, then set results
to 50
and use the contentDetails
to dig the uploaded time of the video and select the newest that way.
Upvotes: 0
Reputation: 3446
You can embed youtube channels or playlists, as said here:
https://stackoverflow.com/a/30089630/7733026
Here is how to embed a channel
<iframe width="600" height="340" src="http://www.youtube.com/embed?max-results=1&controls=0&showinfo=0&rel=0&listType=user_uploads&list=YOUR_CHANNEL_NAME_HERE" frameborder="0" allowfullscreen></iframe>
To get your channel name click "My Channel" and its the line of text after "/user/".
You can also embed playlists with this:
<iframe width="600" height="340" src="https://www.youtube.com/embed/+lastest?list=PLAYLIST_ID" frameborder="0" allowfullscreen></iframe>
Upvotes: 0