k-isabelle
5/31/2018 - 4:29 PM

Fetch YouTube Videos from a Channel

Fetch YouTube Videos from a Channel

// NEEDED: 
// - the channel ID (must be logged in to youtube account then go to "My Channel")
// - a YouTube API key, from https://code.google.com/apis/console (does not need to be from the same account)
// - YouTube API key must be allowed to process requests from the domain in question, or allowed requests from any domain

//https://www.googleapis.com/youtube/v3/search?channelId=UC5sPOQOu3c4uVlL7XtQDwwQ&part=snippet,id&order=date&key=AIzaSyDzEEZ_lM_GwqrXQk-78D7UFq23WrzNyyM

jQuery.getJSON('https://www.googleapis.com/youtube/v3/channels?id=UCmhASLalVvmwhnM1nuyZ9eA&part=contentDetails&key=AIzaSyDzEEZ_lM_GwqrXQk-78D7UFq23WrzNyyM', function(data){
  console.log(data);
  uploadsPlaylist = data.items[0].contentDetails.relatedPlaylists.uploads;
  console.log(uploadsPlaylist);
    
  jQuery.getJSON('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&playlistId=' + uploadsPlaylist + '&key=AIzaSyDzEEZ_lM_GwqrXQk-78D7UFq23WrzNyyM', function(data){
    console.log(data);
    var videos = [];
    for(var i=0; i<data.items.length; i++) {
      videos.push(data.items[i].contentDetails.videoId);
    }
    console.log(videos);
  });

});