How to change page results with YouTube Data API v3

43,452

Solution 1

If you look at the results, you will see a "nextPageToken" item right after "pageInfo". This needs to be passed as the pageToken on your next request.

So if you make a call to this api:

https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=25&order=relevance&q=site%3Ayoutube.com&topicId=%2Fm%2F02vx4&key={YOUR_API_KEY}

You would make a call to this one for the next page:

https://www.googleapis.com/youtube/v3/search?pageToken=CBkQAA&part=snippet&maxResults=25&order=relevance&q=site%3Ayoutube.com&topicId=%2Fm%2F02vx4&key={YOUR_API_KEY}

Solution 2

For example this is your api

https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&channelId=UCrA&maxResults=50&key=AIZsk

Here with this api you will get result like

{
"kind": "youtube#playlistListResponse",
"etag": "\"XpPGQXPLgenD_n8JR4Qk/05DoUs3OS-AxnDI1FJbdM\"",
"nextPageToken": "CDIQAA",
"pageInfo": {
    "totalResults": 585,
    "resultsPerPage": 50
},
"items": [
    {
        "kind": "youtube#playlist",
        "etag": "\"XpPGQXPLgenD_n8JR4Qk/7m0ztlwxvPmRtXjs\"",
        .........
        .........

In this result see "nextPageToken": "CDIQAA",

Now add pageToken parameter to your api

EX:

https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&channelId=UCqrA&maxResults=50&key=AIZsk&pageToken=CDIQAA

Now this is your result, you will get nextPageToken and prevPageToken

{
"kind": "youtube#playlistListResponse",
"etag": "\"XpPGQX4Qk/R3A6jpxuE\"",
"nextPageToken": "CGQQAA",
"prevPageToken": "CDIQAQ",
"pageInfo": {
    "totalResults": 585,
    "resultsPerPage": 50
},
"items": [
    {
        "kind": "youtube#playlist",
        "etag": "\"XpPGQXR4Qk/XsatNRtxJQ\"",

        .........
        .........
Share:
43,452
viktor
Author by

viktor

Updated on March 20, 2020

Comments

  • viktor
    viktor about 4 years

    I'm trying to get video data from the YouTube API (v3) using this example:

    https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&maxResults=25&order=relevance&q=site%253Ayoutube.com&topicId=%252Fm%252F02vx4&_h=1&

    The problem is that I don't understand how to change the page results. For example this query gives me 25 items (maxResults=25) but total results are --> "totalResults": 548669. So the big question here is how to move on page 2 and receive the other 25 results?

  • Moji
    Moji over 8 years
    how do we know we have reached the end of all results?
  • krzysiej
    krzysiej over 8 years
    last page don't have nextPageToken
  • Mustafa
    Mustafa over 8 years
    Ya there is no nextPageToken in the first request with v3 api
  • Mustafa
    Mustafa over 8 years
    @krzysiej i also didnt get the nextPageToken returned. but i removed fields param in my request and i got the next page . so try just including your query and your key as a param in your http request . it will work
  • Matt Koskela
    Matt Koskela over 8 years
    @MurWade I believe krzysiej was just explaining to Manoj Sharma that the last page of results will not have a nextPageToken.
  • Georgi Kovachev
    Georgi Kovachev over 7 years
    How can I move to specific Page? Let's say 10th page?
  • Matt Koskela
    Matt Koskela over 7 years
    @GeorgiKovachev that doesn't appear to be supported, but you could use the publishedAfter and publishedBefore parameters to limit the items you're receiving as an alternative to starting 10 pages deep (depending on your specific use case, of course)
  • puretppc
    puretppc over 6 years
    I wanna list all results at once so I need to get each and every individual nextPageToken? How do I fetch all of the pageToken IDs then?
  • Matt Koskela
    Matt Koskela over 6 years
    @puretppc - you need to programmatically call the API using the nextPageToken in each call until there is no more nextPageToken (meaning you're on the last page)
  • Matt Koskela
    Matt Koskela over 4 years
    Yes, every resource you request will count against your quota. Only request the “parts” that you need to limit the number of units you are using against that quota.
  • user123456
    user123456 almost 4 years
    but why results per page always 50 @iOS