Hello! Trying to get ids from over 50 videos using nextPageToken . But the problem is that every time ids are added to the variable ids from the previous iteration β€” there are more than 50 of them β€” and the query crashes. Are there reasonable ways to prevent this? According to the results you need to get all the videos for the keyword.

 public async Task<ActionResult> Parse(string keyword) { var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "myapikey" }); var nextPageToken = ""; string part = "snippet, statistics"; string ids = string.Empty; StringBuilder sb = new StringBuilder(); var videos = new List<Video>(); while (nextPageToken != null) { var videosListMultipleIdsRequest = youtubeService.Videos.List(part); var searchListRequest = youtubeService.Search.List("snippet"); searchListRequest.Q = keyword; // Replace with your search term. searchListRequest.MaxResults = 50; searchListRequest.PageToken = nextPageToken; var searchListResponse = await searchListRequest.ExecuteAsync(); foreach (var searchResult in searchListResponse.Items) { if (searchResult.Id.Kind.Equals("youtube#video")) { ids = sb.Append($"{searchResult.Id.VideoId},").ToString(); } } ids = ids.Remove(ids.Length - 1); if (!string.IsNullOrEmpty(ids)) { videosListMultipleIdsRequest.Id = ids; } var response = await videosListMultipleIdsRequest.ExecuteAsync(); foreach (var item in response.Items) { videos.Add(item); } ids = string.Empty; nextPageToken = searchListResponse.NextPageToken; } ViewBag.Videos = videos; return View(); } 
  • Well, start with searchListRequest.MaxResults = 100500; - Enikeyschik
  • And what's the point, if the maximum number of results from one page is 50? With the increase - bit.ly/2oLrJaW - Dmitry Bystrov
  • It is not clear what it means: "But the problem is that each time ids are added to the ids variable and the identifiers from the previous iteration β€” there are more than 50 of them β€” and the query crashes.” Explain by example. - Bulson
  • The first thing that came to mind. Once MaxResults = 50, then you need to increase and see what happens. If this is not the case, then tell in exactly which place and how it crashes. Error message, etc. - Enikeyschik
  • @Bulson The first pass through the loop is nexPageToken = "" , sparsil ~ 47 video identifiers, nextPageToken = searchListResponse.NextPageToken (changed the token), the second pass through the cycle will return identifiers from the first pass and add new ones (~ 50), total 97 identifiers will turn out accordingly, the request will fail (number more than 50) - Dmitry Bystrov

0