Google Tasks API BatchRequest触发批量请求重复ID 400报错
Google Tasks BatchRequest 400错误(Duplicate Request ID)问题及解决方案
问题描述
自9月13日起,已稳定运行多年的应用中,Google Tasks BatchRequest更新流程触发400错误,返回提示Duplicate Request ID in Batch Request,但请求中未发现存在重复的请求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" } }]
问题更新
排查后确认谷歌已调整接口规则,要求每个批量请求条目必须携带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; }
内容的提问来源于stack exchange,提问作者David Levinson
相关产品推荐
相关产品推荐

