Getting all videos of a channel using youtube API

后端 未结 3 1384
醉酒成梦
醉酒成梦 2021-02-04 12:05

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:

3条回答
  •  爱一瞬间的悲伤
    2021-02-04 12:27

    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.

    
    
    
        YouTube Channel Listing
        
    
    
        

    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 = "
    "; 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 += "
    " + "" + "" + videotitle + "" + "

    " + videotitle + "

    " + videodescription + "
    " + "" + newdate + "" + "
    "; } content += "
    "; document.getElementById(writediv).innerHTML = content; } catch (ex) { alert(ex.message); } }

提交回复
热议问题