0

I'm trying to load all video IDs of a YouTube playlist and save them in an array in order to access them later.

I started with this:

$.getJSON( "https://www.googleapis.com/youtube/v3/playlistItems", {
  part : 'snippet', 
  playlistId : inputId,
  key: API_KEY
}, function(data) {
  $.each(data.items, function(i, item) {
    var itemId = item.snippet.resourceId.videoId;
    videoIds.push(itemId);
  }
);

Then I noticed only the first 5 video IDs of the input playlist have been saved in my array 'videoIds'. Apparantly, the YouTube API uses pages to limit the amount of items in a GET response. I didn't see a way to increase the amount of items per request. So instead I updated my code by using the response keys 'resultsPerPage', 'totalResults' and 'nextPageToken'. Basically, I now send one GET request for every page of the playlist:

$.getJSON("https://www.googleapis.com/youtube/v3/playlistItems", {
  part : 'snippet', 
  playlistId : inputId,
  key: API_KEY
}, function(data) {
  resultsPerPage = data.pageInfo.resultsPerPage;
  nextPageToken = data.nextPageToken;
  totalResults = data.pageInfo.totalResults;
  $.each(data.items, function(i, item) {
    var itemId = item.snippet.resourceId.videoId;
    videoIds.push(itemId);
  });

  // if playlist items don't fit on a single page
  if (resultsPerPage < totalResults) {
    for (var i = 0; i < totalResults/resultsPerPage; i++) {
      // send the request again...
      $.getJSON("https://www.googleapis.com/youtube/v3/playlistItems", {
        part: 'snippet', 
        playlistId: inputId,
        // but this time, with a pageToken
        pageToken: nextPageToken,
        key: API_KEY
      }, function(data) {
        // debug logging  
        console.log("old token:" + nextPageToken);
        console.log("new token:" + data.nextPageToken);
        // update the nextPageToken for the next iteration
        nextPageToken = data.nextPageToken;
        $.each(data.items, function(i, item) {
          var itemId = item.snippet.resourceId.videoId;
          videoIds.push(itemId);
        });
      });
    }
  }
});

My problem: the value of 'nextPageToken' never changes. This is what my console looks like after I run my code:

old token:CAUQAA script.js:62 
new token:CAoQAA script.js:63
old token:CAoQAA script.js:62
new token:CAoQAA script.js:63
old token:CAoQAA script.js:62
new token:CAoQAA script.js:63
old token:CAoQAA script.js:62
new token:CAoQAA script.js:63

Why is the value of nextPageToken in a GET response the same String as the value I used for pageToken in the correlating GET request? Is there an easier way to get all IDs of a playlist?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
hildebro
  • 549
  • 2
  • 5
  • 20
  • 1
    Look at this older post https://stackoverflow.com/questions/18804904/retrieve-all-videos-from-youtube-playlist-using-youtube-v3-api – Jmartnz Jun 20 '18 at 13:40

1 Answers1

1

YouTube Data API's Videos.list API endpoint returns maximum 50 items per page:

maxResults (unsigned integer)

The maxResults parameter specifies the maximum number of items that should be returned in the result set.

Note: This parameter is supported for use in conjunction with the myRating parameter, but it is not supported for use in conjunction with the id parameter. Acceptable values are 1 to 50, inclusive. The default value is 5.

The way nextPageToken works is like this:

Situation:

You're trying to retrieve a list of 200 videos but YouTube can only return 50 results per pagination.

  1. After a successful first request, YouTube gives you the first 50 videos, so only 150 left. With this first 50 lists of videos comes a nextPageToken, which you can use to retrieve the next 51-100.

  2. So you pass this nextPageToken value to the pageToken property and perform another request, which results in YouTube Data API giving you the next 51-100 videos, so only 100 left. With this 51-100 videos comes a new nextPageToken again which will be used to retrieve the next 101-150 videos. Rinse and repeat.

This will be easier with an actual demo on how to do this so watch this tutorial.

Test it on Google API Explorer youtube.search.list.

stvar
  • 6,551
  • 2
  • 13
  • 28
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • That's how I thought it should work. In my second code example, I'm using the new nextPageToken of the response, but I'm not getting new tokens. It always returns the token for page 2 even though the video IDs of that response are of the videos of page 2 already. – hildebro Jun 21 '18 at 13:10
  • To be clearer: 1st request without a pageToken returns videos 1-5 and nextPageToken 'CAoQAA'. 2nd request with pageToken 'CAoQAA' returns videos 6-10 and the same nextPageToken 'CAoQAA' as before. – hildebro Jun 21 '18 at 13:50
  • you're not using the "new nextPageTokens" . check my answer. watch the video from the youtube. – ReyAnthonyRenacia Jun 21 '18 at 14:15