Youtube api 使用youtube API获取频道的所有视频

Youtube api 使用youtube API获取频道的所有视频,youtube-api,Youtube Api,我想获得一个频道的所有视频,我有它的Id。问题是我只获得频道信息。 这是我正在使用的链接: 该链接用于现在已退役的v2api,因此它不会返回任何数据。相反,您需要使用API的V3。您需要做的第一件事是注册一个API密钥——您可以通过在上创建一个项目,将YouTube数据API设置为“开”,然后创建一个公共访问密钥来完成 因为你已经有了你的用户频道ID,你可以直接从它那里获取视频;但是,请注意,如果您不知道通道ID,可以通过以下方式获取: https://www.googleapis.com/y

我想获得一个频道的所有视频,我有它的Id。问题是我只获得频道信息。 这是我正在使用的链接:


该链接用于现在已退役的v2api,因此它不会返回任何数据。相反,您需要使用API的V3。您需要做的第一件事是注册一个API密钥——您可以通过在上创建一个项目,将YouTube数据API设置为“开”,然后创建一个公共访问密钥来完成

因为你已经有了你的用户频道ID,你可以直接从它那里获取视频;但是,请注意,如果您不知道通道ID,可以通过以下方式获取:

https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername={username}&key={YOUR_API_KEY}
使用频道ID,您可以通过搜索端点从频道获取所有视频,如下所示:

https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId={channel id here}&maxResults=25&key={YOUR_API_KEY}
在这种情况下,按日期排序与按“已发布”排序的旧V2参数相同


在搜索频道时,还可以使用许多其他参数来检索视频;有关更多详细信息,请参阅。

我想我会使用JavaScript分享我的最终结果。它使用Google YouTube API键和用户名获取频道ID,然后将视频和显示在列表中,并显示到给定的div标记

<!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>

YouTube频道列表
函数getJSONData(您的URL){
var Httpreq=new-XMLHttpRequest();
试一试{
Httpreq.open(“GET”,yourUrl,false);
Httpreq.send(空);
}捕获(ex){
警报(例如消息);
}
返回Httpreq.responseText;
}
函数showVideoList(用户名、writediv、maxnumbervideos、apikey){
试一试{
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;//默认值,中等或高
//频道头
document.getElementById(writediv).innerHTML+=“”
+ ""
+“”+channeldescription+“”
+ "";
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;
//视频列表
对于(var i=0;i”
+ ""
+“”+videodescription+“”
+ "";
}
}捕获(ex){
警报(例如消息);
}
}
showVideoList(“用户名”、“视频”、25、“您的API密钥”);
此外,我还编写了一个函数,用于处理使用通道ID而不是基于用户名的帐户的情况

这是代码:

        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);
        }
    }
函数showVideoListChannel(channelid、writediv、maxnumbervideos、apikey){
试一试{
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=“”;
对于(var i=0;i12){
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”;
}否则{
newdate=newdate.getMonth()+1+“/”+newdate.getDate()+“/”+newdate.getFullYear()+“+newdate.getHours()+”:“+min+“AM”;
}
var videothumbnail=videos[i]。snippet.thumbnails.default.url;//默认值,中等或高
内容+=“
” + "" +“+videodescription+”
” +“”+newdate+“” + ""; } 内容+=”; document.getElementById(writediv).innerHTML=content; }捕获(ex){ 警报(例如消息); } }
这很容易
<?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}