How to change page results with YouTube Data API v3
Asked Answered
P

2

39

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?

Plauen answered 5/1, 2013 at 15:50 Comment(0)
C
80

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}
Chile answered 28/1, 2013 at 18:46 Comment(10)
how do we know we have reached the end of all results?Dejected
last page don't have nextPageTokenStibine
Ya there is no nextPageToken in the first request with v3 apiIgnescent
@Stibine 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 workIgnescent
@MurWade I believe krzysiej was just explaining to Manoj Sharma that the last page of results will not have a nextPageToken.Chile
How can I move to specific Page? Let's say 10th page?Overact
@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)Chile
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?Ricard
@Ricard - 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)Chile
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.Chile
O
7

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\"",

        .........
        .........
Oza answered 19/3, 2019 at 11:33 Comment(2)
but why results per page always 50 @iOSMuscatel
@Muscatel maxResults defaulte 5, and maximum is 50Diwan

© 2022 - 2024 — McMap. All rights reserved.