Dav*_*son 5 c# json google-tasks google-tasks-api
从 9 月 13 日开始,Google Tasks BatchRequest 更新工作流程将在多年来保持稳定的应用程序中触发 400 错误返回“批量请求中的重复请求 ID”。我在请求中找不到任何指示重复请求 ID 的内容。有人知道怎么回事吗?谷歌改变了什么吗?
这是我在发送简单的批处理任务插入请求时收到的响应副本......
[{
"error": {
"code": 400,
"message": "Duplicate Request ID in Batch Request: ",
"errors": [
{
"message": "Duplicate Request ID in Batch Request: ",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}]
Run Code Online (Sandbox Code Playgroud)
在调查此问题时,我发现 Google 进行了更改,现在要求Content-ID
每个批量请求项上都有标头。当前使用 .Net 类时未设置此标头 Google.Apis.Requests.BatchRequest
。
为了解决这个问题,我能够创建一个新的 Google.Apis.Requests.BatchRequest 本地实现,并注入一个“Content-ID”标头,然后创建每个请求条目。
private static long _id = 0;
[VisibleForTestOnly]
internal static async Task<HttpContent> CreateIndividualRequest(IClientServiceRequest request)
{
HttpRequestMessage requestMessage = request.CreateRequest(false);
string requestContent = await CreateRequestContentString(requestMessage).ConfigureAwait(false);
var content = new StringContent(requestContent);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/http");
content.Headers.Add("Content-ID", (_id++).ToString());
return content;
}
Run Code Online (Sandbox Code Playgroud)
感谢您解决这个问题!标题Content-ID
解决了这个问题。
Google.Apis.Requests.BatchRequest
但是,您可以将 a 注入HttpExecuteInterceptor
到底层服务中,然后由批处理请求调用,而不是创建新的实现。
创建拦截器:
public class GoogleBatchInterceptor : IHttpExecuteInterceptor
{
public Task InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Content is not MultipartContent multipartContent)
{
return Task.CompletedTask;
}
foreach (var content in multipartContent)
{
content.Headers.Add("Content-ID", Guid.NewGuid().ToString());
}
return Task.CompletedTask;
}
}
Run Code Online (Sandbox Code Playgroud)
将拦截器添加到服务中:
CalendarService calendarService = CreateTheCalendarService();
calendarService.HttpClient.MessageHandler.AddExecuteInterceptor(new GoogleBatchInterceptor());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
660 次 |
最近记录: |