Archer Archer - 3 months ago 25
Javascript Question

Youtube v3 API - pageToken doesn't work

I'm struggling a bit with the Youtube v3 API, specifically the

pageToken
concept. It's one I'm familiar with from dozens of other APIs, but this one doesn't seem to work.

I make a search request and get the expected results, along with a
nextPageToken
since there are more than the requested number of results.

I then perform a request and pass that pageToken value, but I start to get incorrect results after that.

Here's the code I have so far (I do load the gapi client in another piece of code)...

function getVideos(pageToken) {
var options = {
channelId: "UC1HkhNoUIYYyaruWmxRRUsQ",
q: "GTA V - Stunt practise",
part: "snippet",
maxResults: 10,
order: "date"
};

if (pageToken) {
options.pageToken = pageToken;
}

var request = gapi.client.youtube.search.list(options);

request.execute(function(response) {
var items = response.result.items;
if (!items.length) return;

for (var i = 0; i < items.length; i++) {
var item = items[i];
addVideo({
id: item.id.videoId,
title: item.snippet.title,
description: item.snippet.description,
date: new Date(item.snippet.publishedAt),
thumbnail: item.snippet.thumbnails.high.url
});
}

if (response.nextPageToken) {
getVideos(response.nextPageToken);
}
});
}

function addVideo(video) {
console.log(video.title);
}

getVideos();


The first batch of results shows the following:

GTA V - Stunt practise 21
GTA V - Stunt practise 20
GTA V - Stunt practise 19
GTA V - Stunt practise 18 (version 1)
GTA V - Stunt practise 18 (version 2)
GTA V - Stunt practise 17
GTA V - Stunt practise 16
GTA V - Stunt practise 15
GTA V - Stunt practise 14
GTA V - Stunt practise 13


which is correct (well, close enough). The second batch of results, though, is just completely wrong...

GTA V - Stunt practise 01
GTA V - Stunt practise 07
GTA V - Stunt practise 02
GTA V - Stunt practise 08


There's not enough of them, they're not the expected videos and they're not in order.

Has anyone had any experience with the Youtube API, particularly using page tokens with search results? I'd really appreciate some help with this.

Answer

I now believe this to be a bug. The reason I believe that, and the reason I'm posting an answer, is because it works perfectly with a playlist...

function getVideos(prev) {
    var options = {
        part: "snippet",
        playlistId: "PLvocdfFatwmphP9Lj4Mzpht4H75PtLA-p",
        maxResults: 10
    };

    if (prev) {
        options.pageToken = prev.nextPageToken;
    }

    var request = gapi.client.youtube.playlistItems.list(options);

    request.execute(function(response) {
        var items = response.result.items;
        if (!items.length) return;

        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            addVideo({
                id: item.id.videoId,
                title: item.snippet.title,
                description: item.snippet.description,
                date: new Date(item.snippet.publishedAt),
                thumbnail: item.snippet.thumbnails.high.url
            });
        }

        if (response.nextPageToken) {
            getVideos(response);
        }
    });
}

function addVideo(video) {
    console.log(video.title);
}

getVideos();

This means I have to manually add each video to the playlist when I upload it, which is what I was trying to avoid. It's not really a dynamic process if I have to get involved!

This answer is posted in case somebody stumbles across this page with the same issue that I had.

Comments