Search code examples
javascriptreactjsreact-reduxyoutube-data-apiredux-saga

How to get views and comment count when using Youtube data api -v3 playlistItems


I'm trying to get some data using Youtube data api -v3 playlistItems. I'm able to get all the data that comes with playlistItems perfectly. However, I need more data than what playlistItems offers. For example, I would like to get the view counts, comment count, and all the statistics

I know I could use /youtube/v3/videos to get the statistics but I have been trying this and it is not working for me. Please help. Thanks.

export function buildVideosRequest(amount = 12, loadDescription = false, nextPageToken) {
  let fields = 'nextPageToken,prevPageToken,items(contentDetails/videoId,id,snippet(channelId,channelTitle,publishedAt,thumbnails/medium,title)),pageInfo(totalResults)';
  if (loadDescription) {
    fields += ',items/snippet/description';
  }
  return buildApiRequest('GET',
    '/youtube/v3/playlistItems',
    {
      part: 'snippet,contentDetails',
      maxResults: amount,
      playlistId: 'PLvahqwMqN4M0zIUkkXUW1JOgBARhbIz2e',
      pageToken: nextPageToken,
      fields,
    }, null);
}


Solution

  • Upon invoking the PlaylistItems.list endpoint, you obtain a result set of which each item is a playlistItems resource JSON object.

    That JSON object doesn't contain the info you're interested in (view count, comment count, etc). This kind of info -- as you alluded yourself -- is obtainable through the Videos.list API endpoint.

    That is that you have to collect all video IDs that you're interested in into an array, then invoke repeatedly the Videos.list endpoint, passing to it an id parameter assigned properly.

    Note that this endpoint's id property allows you to reduce the number of endpoint calls, since the id may be specified as a comma-separated list of video IDs (at most 50). Hence, if you have for example an array of 114 video ID's, then you may issue only 3 calls to Videos.list.