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(); }
searchListRequest.MaxResults = 100500;- EnikeyschiknexPageToken = "", 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