Getting all videos of a channel using youtube API
Asked Answered
T

4

13

I want to get all videos of a single channel that i have its Id. The problem that I am getting only the channel informations. this is the link that I am using:

https://gdata.youtube.com/feeds/api/users/UCdCiB_pNQpR0M_KkDG4Dz5A?v=2&alt=json&q=goal&orderby=published&max-results=10

Thermobarograph answered 6/5, 2015 at 15:33 Comment(2)
this link doesn't work !!Arrogance
yes you have right this is the link that show channel informations : gdata.youtube.com/feeds/api/users/… but when i add some parameters in URL, it dosen't work !Thermobarograph
D
57

That link is for the now-retired V2 API, so it will not return any data. Instead, you'll want to use V3 of the API. The first thing you'll need to do is register for an API key -- you can do this by creating a project at console.developers.google.com, setting the YouTube data API to "on," and creating a public access key.

Since you have your user channel ID already, you can jump right into getting the videos from it; note, however, that if you ever don't know the channel ID, you can get it this way:

https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername={username}&key={YOUR_API_KEY}

With the channel ID, you can get all the videos from the channel with the search endpoint, like this:

https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId={channel id here}&maxResults=25&key={YOUR_API_KEY}

In this case, ordering by date is the same as the old V2 parameter for ordering by "published."

There are also a lot of other parameters you can use to retrieve videos while searching a channel; see https://developers.google.com/youtube/v3/docs/search/list for more details.

Downstage answered 6/5, 2015 at 19:3 Comment(12)
an error message is genereted :"reason": "keyInvalid", what's my api Key ??Thermobarograph
You need to go to console.developers.google.com -- there you'll create a project. When the project is created, select it, and choose the "APIs and Auth" menu entry in the left sidebar. This will display all the possible APIs ... you'll want to choose "YouTube Data API" and set it to "on." Finally, choose the "Credentials" section in the left sidebar, where you'll be able to create a "public access key."Downstage
it works :) thanks a lot :) can you tell me how can i get any channel ID so i can add it into the URL ?Thermobarograph
Just use the first URL above (that hits the 'channels' endpoint); if you pass any username, you can get back that user's channel ID.Downstage
This might be old, because the first endpoint just gives a short snippet description about the channel, not the video listGrudging
You're a lifesaver! This is the correct answer for sure.Pulsar
Can i get the counts of likes and comments of each video by this api? I tried statistics parameter but it responded with bad request 400Stamen
only return 25 video result, how to return all video result ?Highborn
The second example for some reason uses like 1200 quota each request... even if maxResults is set to 2Dorene
This is using the search API, which uses 100 quota (out of the default 10,000) for the day.Cyr
Search is using 100 quotas, but channels 1. developers.google.com/youtube/v3/determine_quota_costLewie
The search endpoint is very expensive though.Elegit
F
5

I thought I would share my final result using JavaScript. It uses the Google YouTube API key and UserName to get the channel ID, then pulls the videos and displays in a list to a given div tag.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>YouTube Channel Listing</title>
    <script type="text/javascript">
        function getJSONData(yourUrl) {
            var Httpreq = new XMLHttpRequest();
            try {
                Httpreq.open("GET", yourUrl, false);
                Httpreq.send(null);
            } catch (ex) {
                alert(ex.message);
            }
            return Httpreq.responseText;
        }
        function showVideoList(username, writediv, maxnumbervideos, apikey) {
            try {
                document.getElementById(writediv).innerHTML = "";
                var keyinfo = JSON.parse(getJSONData("https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=" + username + "&key=" + apikey));
                var userid = keyinfo.items[0].id;
                var channeltitle = keyinfo.items[0].snippet.title;
                var channeldescription = keyinfo.items[0].snippet.description;
                var channelthumbnail = keyinfo.items[0].snippet.thumbnails.default.url; // default, medium or high
                //channel header
                document.getElementById(writediv).innerHTML += "<div style='width:100%;min-height:90px;'>"
                    + "<a href='https://www.youtube.com/user/" + username + "' target='_blank'>"
                    + "<img src='" + channelthumbnail + "' style='border:none;float:left;margin-right:10px;' alt='" + channeltitle + "' title='" + channeltitle + "' /></a>"
                    + "<div style='width:100%;text-align:center;'><h1><a href='https://www.youtube.com/user/" + username + "' target='_blank'>" + channeltitle + "</a></h1>" + channeldescription + "</div>"
                    + "</div>";
                var videoinfo = JSON.parse(getJSONData("https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=" + userid + "&maxResults=" + maxnumbervideos + "&key=" + apikey));
                var videos = videoinfo.items;
                var videocount = videoinfo.pageInfo.totalResults;
                // video listing
                for (var i = 0; i < videos.length; i++) {
                    var videoid = videos[i].id.videoId;
                    var videotitle = videos[i].snippet.title;
                    var videodescription = videos[i].snippet.description;
                    var videodate = videos[i].snippet.publishedAt; // date time published
                    var videothumbnail = videos[i].snippet.thumbnails.default.url; // default, medium or high
                    document.getElementById(writediv).innerHTML += "<hr /><div style='width:100%;min-height:90px;'>"
                        + "<a href='https://www.youtube.com/watch?v=" + videoid + "' target='_blank'>"
                        + "<img src='" + videothumbnail + "' style='border:none;float:left;margin-right:10px;' alt='" + videotitle + "' title='" + videotitle + "' /></a>"
                        + "<h3><a href='https://www.youtube.com/watch?v=" + videoid + "' target='_blank'>" + videotitle + "</a></h3>" + videodescription + ""
                        + "</div>";
                }
            } catch (ex) {
                alert(ex.message);
            }
        }
    </script>
</head>
<body>
    <div id="videos"></div>
    <script type="text/javascript">
        showVideoList("USER_NAME", "videos", 25, "YOUR_API_KEY");
    </script>
</body>
</html>

ADDITION - I also wrote a function to handle if you are using a channel ID instead of a UserName based account.

Here is that code:

        function showVideoListChannel(channelid, writediv, maxnumbervideos, apikey) {
        try {
            document.getElementById(writediv).innerHTML = "";
            var vid = getJSONData("https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=" + channelid + "&maxResults=" + (maxnumbervideos + 1) + "&key=" + apikey);
            var videoinfo = JSON.parse(vid);
            var videos = videoinfo.items;
            var videocount = videoinfo.pageInfo.totalResults;
            var content = "<div style='height:600px;overflow-y:auto;'>";
            for (var i = 0; i < videos.length - 1; i++) {
                var videoid = videos[i].id.videoId;
                var videotitle = videos[i].snippet.title;
                var videodescription = videos[i].snippet.description;
                var videodate = videos[i].snippet.publishedAt; // date time published
                var newdate = new Date(Date.parse((videodate + " (ISO 8601)").replace(/ *\(.*\)/, "")));
                var min = newdate.getMinutes();
                if (min < 10) {
                    min = "0" + min;
                }
                if (newdate.getHours() > 12) {
                    newdate = newdate.getMonth() + 1 + "/" + newdate.getDate() + "/" + newdate.getFullYear() + " " + (newdate.getHours() - 12) + ":" + min + " PM";
                } else if (newdate.getHours() == 12) {
                    newdate = newdate.getMonth() + 1 + "/" + newdate.getDate() + "/" + newdate.getFullYear() + " " + newdate.getHours() + ":" + min + " PM";
                } else {
                    newdate = newdate.getMonth() + 1 + "/" + newdate.getDate() + "/" + newdate.getFullYear() + " " + newdate.getHours() + ":" + min + " AM";
                }
                var videothumbnail = videos[i].snippet.thumbnails.default.url; // default, medium or high
                content += "<hr /><div style='width:100%;min-height:90px;'>"
                    + "<a href='https://www.youtube.com/watch?v=" + videoid + "' target='_blank'>"
                    + "<img src='" + videothumbnail + "' style='border:none;float:left;margin-right:10px;' alt='" + videotitle + "' title='" + videotitle + "' /></a>"
                    + "<h3><a href='https://www.youtube.com/watch?v=" + videoid + "' target='_blank'>" + videotitle + "</a></h3>" + videodescription + "<br />"
                    + "<span style='color:#738AAD;font-size:Small;'>" + newdate + "</span>"
                    + "</div>";
            }
            content += "</div>";
            document.getElementById(writediv).innerHTML = content;
        } catch (ex) {
            alert(ex.message);
        }
    }
Footless answered 17/11, 2015 at 21:10 Comment(4)
Hi please which username you are using in this source code ? @Dr. Aaron DishnoHillard
Example, if the YouTube path is: youtube.com/user/SBCountyParks then SBCountyParks is the username. I wrote the page here: sbcounty.gov/Main/Pages/YouTube.aspx notice when you click Regional Parks, it shows the Videos from that link.Footless
Thanks you so much .. You saved my life , Is there way to increase Quota of Youtube API then how to get all result instead of 25 (Fixed Number) then Thanks againBradski
On this line, I am telling it to bring back 25 videos. Try changing the number. It looks like the API will accept up to 50 maxResults. showVideoList("USER_NAME", "videos", 25, "YOUR_API_KEY"); developers.google.com/youtube/v3/docs/videos/listFootless
W
3

Here is the way to get all videos with only 2 quotas using YouTube Data API (v3)

First of all do a list on channels with part=contentDetails (1 quota) :

https://youtube.googleapis.com/youtube/v3/channels?part=contentDetails&id=[CHANNEL_ID]&key=[YOUR_API_KEY]

You will get this result :

{
  ...
  "items": [
    {
      ...
      "contentDetails": {
        "relatedPlaylists": {
          "likes": "",
          "uploads": "UPLOADS_PLAYLIST_ID"
        }
      }
    }
  ]
}

Then take UPLOADS_PLAYLIST_ID and do a list on playlistItems with part=contentDetails (1 quota):

https://youtube.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=[UPLOADS_PLAYLIST_ID]&key=[YOUR_API_KEY]

You will get this result:

{
  ...
  "items": [
    {
      ...
      "contentDetails": {
        "videoId": "VIDEO_ID",
        "videoPublishedAt": "2022-10-27T16:00:08Z"
      }
    },
    ...
  ],
  "pageInfo": {
    "totalResults": 5648,
    "resultsPerPage": 5
  }
}

You got the list of the videos under items

You can of course change the size of this list by adding maxResults=50 (max value is 50)

Woolson answered 29/10, 2022 at 16:32 Comment(2)
Add snippet if you want more info than only the videoId and published date: ... v3/playlistItems?part=contentDetails,snippet& ...Terpsichorean
Fed up of search API, I almost fell for your solution but realised that channel section response doesn't have uploads identifier. Can you help ?Cariecaries
C
1

It is very easy method to get channel videos using your channel API key:
Step 1: You must have an YouTube account.
Step 2: Create your YouTube channel API key
Step 3: Create project console.developers.google.com,

<?php
$API_key    = 'Your API key'; //my API key dei;
$channelID  = 'Your Channel ID'; //my channel ID
$maxResults = 5;
$video_list = 
json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search? 
order=date&part=snippet&channelId='.$channelID.
'&maxResults='.$maxResults.'&key='.$API_key.''));
?>



Example : https://www.googleapis.com/youtube/v3/channelspart=snippet&forUsername= 
{username}&key={YOUR_API_KEY}
Contradance answered 15/9, 2020 at 12:4 Comment(1)
For the people who will try this method in the future; the Search endpoint uses 100 quotas. Use it with caution.Lewie

© 2022 - 2024 — McMap. All rights reserved.