Toniq
Toniq

Reputation: 5016

Youtube api V3 nextPageToken repeats

I am getting some weird results using url to retrieve youtube playlist items. First of all, youtube playlist can contain max of 200 playlist items.

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,status,contentDetails&maxResults=50&playlistId=PLFgquLnL59alCl_2TQvOiD5Vgm1hCaGSI&key=API_KEY

  1. When I run this I get correct results (50 items returned, total results 200, results per page 50, nextPageToken: "CDIQAA")

  2. Then I keep running new request always with last nextPageToken:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,status,contentDetails&maxResults=49&playlistId=PLFgquLnL59alCl_2TQvOiD5Vgm1hCaGSI&key=API_KEY&pageToken=CDEQAA

100 results, nextPageToken: "CGQQAA",

  1. https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,status,contentDetails&maxResults=49&playlistId=PLFgquLnL59alCl_2TQvOiD5Vgm1hCaGSI&key=API_KEY&pageToken=CGQQAA

150 results so far, nextPageToken: "CDIQAA"

Now this nextPageToken keeps repeating its the same at first nextPageToken, why, I still havent retrieved all 200 results?

Upvotes: 0

Views: 2101

Answers (1)

Wishes
Wishes

Reputation: 61

I guess there some logical issue in your code, I've got CJYBEAA token after third request. Here function that works fine with your playlist ID and returns whole 200 video IDs:

def getPlaylistVideosIDs(playlist_id):
    videos_IDs = []
    search = YOUR_YOUTUBE_KEY.playlistItems().list(part='snippet', playlistId=playlist_id,
                                              maxResults=50).execute()

    try:
        nextPageToken = search['nextPageToken']
    except KeyError:
        nextPageToken = None

    for item in search['items']:
        videos_IDs.append(item['snippet']['resourceId']['videoId'])

    while (nextPageToken):
        search = YOUR_YOUTUBE_KEY.playlistItems().list(pageToken=nextPageToken, part='snippet',
                                                  playlistId=playlist_id,
                                                  maxResults=50).execute()
        for item in search['items']:
            videos_IDs.append(item['snippet']['resourceId']['videoId'])

        try:
            nextPageToken = search['nextPageToken']
        except KeyError:
            break

    return videos_IDs

Upvotes: 2

Related Questions