Google.Apis.YouTube.v3编译频道统计信息失败“ uri字符串太长”

时间:2019-05-23 19:51:51

标签: youtube-api youtube-data-api

我正在尝试为频道列表和第一页编译频道统计信息,但是当我使用令牌调用下一页时,它给我一个错误,即URI字符串太长。

我正在使用.NET Core 2.2和Google.Apis.YouTube.v2 V1.39.0.1572。我使用的代码非常简单:

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey = Startup.Configuration["YTConfigurations:ApiKey"],
                    ApplicationName = this.GetType().ToString()
                });

                ChannelsResource.ListRequest channelsListRequest = youtubeService.Channels.List("snippet,statistics,brandingSettings,topicDetails");
                channelsListRequest.Id = string.Join(",", channelEntities.Select(v => v.YTChannelId));

                channelsListRequest.MaxResults = 50;

                do
                {
                    pageCounter++;
                    channelListResponse = channelsListRequest.Execute();//here is the error after page 1

                    foreach (var listResult in channelListResponse.Items)
                    {
                        channelEntity = channelEntities.FirstOrDefault(v => v.YTChannelId == listResult.Id);
                        channelEntity = Mapper.Map(listResult, channelEntity);   
                        _repository.UpdateChannel(channelEntity);
                    }

                    if (channelListResponse.NextPageToken != null)
                    {
                        channelsListRequest.PageToken = channelListResponse.NextPageToken;
                    }
                } while (channelListResponse.Items.Count == 50 && channelListResponse.NextPageToken != null);

执行该命令后,我会得到:

System.UriFormatException: Invalid URI: The Uri string is too long.
   at System.UriHelper.EscapeString(String input, Int32 start, Int32 end, Char[] dest, Int32& destPos, Boolean isUriString, Char force1, Char force2, Char rsvd)
   at System.Uri.EscapeDataString(String stringToEscape)
   at Google.Apis.Requests.RequestBuilder.<>c.<BuildUri>b__25_0(KeyValuePair`2 x) in C:\Apiary\2019-05-01.11-08-18\Src\Support\Google.Apis.Core\Requests\RequestBuilder.cs:line 108
   at System.Linq.Enumerable.SelectListIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Google.Apis.Requests.RequestBuilder.BuildUri() in C:\Apiary\2019-05-01.11-08-18\Src\Support\Google.Apis.Core\Requests\RequestBuilder.cs:line 107
   at Google.Apis.Requests.RequestBuilder.CreateRequest() in C:\Apiary\2019-05-01.11-08-18\Src\Support\Google.Apis.Core\Requests\RequestBuilder.cs:line 332
   at Google.Apis.Requests.ClientServiceRequest`1.CreateRequest(Nullable`1 overrideGZipEnabled) in C:\Apiary\2019-05-01.11-08-18\Src\Support\Google.Apis\Requests\ClientServiceRequest.cs:line 257
   at Google.Apis.Requests.ClientServiceRequest`1.ExecuteUnparsedAsync(CancellationToken cancellationToken) in C:\Apiary\2019-05-01.11-08-18\Src\Support\Google.Apis\Requests\ClientServiceRequest.cs:line 229
   at Google.Apis.Requests.ClientServiceRequest`1.Execute() in C:\Apiary\2019-05-01.11-08-18\Src\Support\Google.Apis\Requests\ClientServiceRequest.cs:line 167
   at ChannelHarvester.Controllers.ChannelHarvester.extractStats(ICollection`1 channelEntities) in C:\Guayaba Projects\YT_ChannelHarvester\ChannelHarvester\Controllers\ChannelHarvester.cs:line 106

我做错什么了吗?如果有什么可以解决的问题,请告诉我。 谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,很抱歉打扰你们了。我发现它是什么,在下面的代码中,我以为我只加入了50个ID,但是显然在某些情况下,我要发送更多的ID。

channelsListRequest.Id = string.Join(",", channelEntities.Select(v => v.YTChannelId));

所以我想我们可以关闭这个