Pulling a feed of youtube videos with statistics and duration data
<p>I'm currently trying to use the Youtube V3 API to generate a feed of the most recently uploaded videos in a channel. Currently I've got this <em>mostly</em> working, however I can't for the life of me get two important bits of data pulled through - video views and video duration. I understand that these bits of data belong to the "videos" endpoint, but i'm not sure how to integrate this into the Javascript API example that was provided on Google's Youtube API website. Here is my code below - any help would be greatly appreciated:</p>

<pre><code>// Define some variables used to remember state.
var playlistId, nextPageToken, prevPageToken;

// After the API loads, call a function to get the uploads playlist ID.
function handleAPILoaded() {
  requestUserUploadsPlaylistId();
}

// Call the Data API to retrieve the playlist ID that uniquely identifies the
// list of videos uploaded to the currently authenticated user's channel.
function requestUserUploadsPlaylistId() {
  // See https://developers.google.com/youtube/v3/docs/channels/list
  var request = gapi.client.youtube.channels.list({
    mine: true,
    part: 'contentDetails, statistics'
  });
  request.execute(function(response) {
    playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
    requestVideoPlaylist(playlistId);
  });
}

// Retrieve the list of videos in the specified playlist.
function requestVideoPlaylist(playlistId, pageToken) {
  $('#video-container').html('');
  var requestOptions = {
    playlistId: playlistId,
    part: 'snippet, contentDetails',
    maxResults: 10
  };
  if (pageToken) {
    requestOptions.pageToken = pageToken;
  }
  var request = gapi.client.youtube.playlistItems.list(requestOptions);
  request.execute(function(response) {
    // Only show pagination buttons if there is a pagination token for the
    // next or previous page of results.
    nextPageToken = response.result.nextPageToken;
    var nextVis = nextPageToken ? 'visible' : 'hidden';
    $('#next-button').css('visibility', nextVis);
    prevPageToken = response.result.prevPageToken
    var prevVis = prevPageToken ? 'visible' : 'hidden';
    $('#prev-button').css('visibility', prevVis);

    var playlistItems = response.result.items;
    if (playlistItems) {
      $.each(playlistItems, function(index, item) {
        displayResult(item.snippet);
        displayContentDetails(item.contentDetails);
        displayStats(item.statistics);
      });
    } else {
      $('#video-container').html('Sorry you have no uploaded videos');
    }
  });
}

// Create a listing for a video.
function displayResult(videoSnippet) {
  var title = videoSnippet.title;
  var videoId = videoSnippet.resourceId.videoId;
  var publishedAt = videoSnippet.publishedAt;
  var videoThumbURL = videoSnippet.thumbnails.high.url;
  var publishedUTC = new Date(publishedAt);
  var publishedDay = publishedUTC.getUTCDay();
  var publishedMonth = publishedUTC.getUTCMonth();
  var publishedYear = publishedUTC.getUTCFullYear();
  var a = moment(publishedUTC);
  var b = moment(Date.now());
  var timeFrom = a.from(b);
  $('#video-container').append('&lt;a href=\"https://youtu.be/'+ videoId +'\"&gt;&lt;li&gt;&lt;img src='+videoThumbURL+'&gt;&lt;br&gt;&lt;p&gt;' + title + ' - ' + timeFrom + '&lt;/p&gt;&lt;/li&gt;&lt;/a&gt;');
}

// Create a listing for a video.
function displayContentDetails(stuff) {
  var videoEndsAt = stuff.duration;
  $('#video-container li').append('&lt;span class="endsAt"&gt;'+ videoEndsAt +'&lt;/span&gt;');
}

function displayStats(vidStats) {
  var videoStats = vidStats.viewCount;
  $('#video-container li').append('&lt;span class="videoStats"&gt;'+ videoStats +'&lt;/span&gt;');
}


// Retrieve the next page of videos in the playlist.
function nextPage() {
  requestVideoPlaylist(playlistId, nextPageToken);
}

// Retrieve the previous page of videos in the playlist.
function previousPage() {
  requestVideoPlaylist(playlistId, prevPageToken);
}
</code></pre>