1
votes

I am using Youtube Api to get all videos details of channel

https://www.googleapis.com/youtube/v3/search?part=snippet,id&key=XXXXXXXX&channelId=UCq-Fj5jknLsUf-MWSy4_brA&order=date&maxResults=50

You can see response

In the first response, I can see there are 761525 results and nextPageToken is also there but using that next page token I can max 300 to 400 video details after that API not giving me any result.

So is there any other to get all the videos details? Or anything is missing by me?

2

2 Answers

0
votes

YouTube returns a soft limit of 500 videos for any search request.

If there are more than that in totalResults, you can split the search query using the publishedAfter and publishedBefore filters to loop through dates by day/week/month etc., making sure each 'time window' returns less than the 500 limit. Eventually, you will get all results.

0
votes

After the first time you request an API call, for example this: https://www.googleapis.com/youtube/v3/search?part=snippet,id&key=XXXXXXXX&channelId=UCq-Fj5jknLsUf-MWSy4_brA&order=date&maxResults=50

You need to fetch the nextPageToken parameter of that (1st) result , and feed it to the next (2nd) request. Let's suppose the nextPageToken of the first result is ABCDEFG, your next request must be like this: https://www.googleapis.com/youtube/v3/search?part=snippet&pageToken=ABCDEFG,id&key=XXXXXXXX&channelId=UCq-Fj5jknLsUf-MWSy4_brA&order=date&maxResults=50

See that after snippet I've added &pageToken=ABCDEFG. And then you take again the string of the parameter nextPageToken of this (2nd) result and feed it to your next (3rd) request. So you need to do this with a while loop until the nextPageToken is null, which means you've hit the border number (in your case 761525 results). Or you can fix this by a certain number of results that you get.

Summary:

  1. Request your first API call
  2. Make a loop
  3. Take the nextPageToken value
  4. For your next requests, add after snippet this part &pageToken=
  5. Write the value of nextPageToken after &pageToken=
  6. Repeat from step 2 until a certain condition (until you get all the results or just a certain number of results)