Youtube data API version 3 pagination

2019-04-14 10:15发布

问题:

I understand that youtube response contains the next and previous page tokens and we can use those to go to previous and next pages.

eg :

https://www.googleapis.com/youtube/v3/search?&key={key}&part=snippet&maxResults=20&order=viewCount&q=abc&type=video&videoDuration=long&videoType=movie&pageToken=Cd323A

My question is how can I navigate to nth page of particular search?

Please note that some people may see this is impossible. But I have seen some site implemented this.

回答1:

Youtube API only provides NextPageToken and PreviousPageToken, that's it, sorry: see documentation here: https://developers.google.com/youtube/v3/docs/videos/list

You can still make a pagination item but you will have to implement it by calling multiple times the URL, passing the NextPageToken as pageToken in your requestion every time, and caching results.

By the way, this question is already answered here (see the 1st point of the answer): youtube data api v3 php search pagination?

Quoting them:

In particular your case where you need 50 pages per page and you are showing 3 pagination like (1,2,NEXT) then you need to fetch results two times. Both the results you will keep in cache so for page 1 and 2 results will be retrieved from cache. For next you make it sure that you are making query google again by sending nextPageToken.

Thus to show pagination 1-n and every page 50 results then you need to make n-1 queries to google api. But if you are showing 10 results per page then you cane make single query of 50 results using which you can show first 5 pages (1-5) with the help of retrieved results and at next you should again send next page token like above.

NOTE- Google youtube api provide 50 results max.



回答2:

nextPageToken and prevPageToken comes with the api request.

Now you save the token in an array and then insert it as a nextPageToken in another api call and you will get the next page. The same way you will get a prevPageToken when u get the next page results.

Hope it helps.