批量添加O365用户私人联系人时遇400错误,请求排查
批量添加O365私人联系人返回400 BadRequest错误排查
我尝试通过批量请求为O365用户添加私人联系人,参考Microsoft Graph官方批量请求示例编写代码后,运行返回400 BadRequest错误。目前批量删除联系人功能正常,怀疑问题出在以下代码段:
var requestUrl = graphServiceClient.Users[user.Id].Contacts.RequestUrl; System.Console.WriteLine($"RequestURL: {requestUrl}"); var request = new HttpRequestMessage(HttpMethod.Post, requestUrl) { Content = new StringContent(JsonConvert.SerializeObject(contact.toGraphContact()), Encoding.UTF8, "application/json") };
完整实现代码
public static void importRelevantContactsToUserWithBatch(Manager manager) { List<Contact> succesful = new List<Contact>(); List<Contact> failed = new List<Contact>(); int limit = 1; int counter = 0; var user = users.Where(x => x.Mail == manager.Email).FirstOrDefault(); var contacts = getRelevantContactsForManager(manager); foreach (var contact in contacts) { var batchRequestContent = new BatchRequestContent(); var requestUrl = graphServiceClient.Users[user.Id].Contacts.RequestUrl; System.Console.WriteLine($"RequestURL: {requestUrl}"); var request = new HttpRequestMessage(HttpMethod.Post, requestUrl) { Content = new StringContent(JsonConvert.SerializeObject(contact.toGraphContact()), Encoding.UTF8, "application/json") }; System.Console.WriteLine(JsonConvert.SerializeObject(contact.toGraphContact())); var requestStep = new BatchRequestStep(counter.ToString(), request, null); batchRequestContent.AddBatchRequestStep(requestStep); var returnedResponse = graphServiceClient.Batch.Request().PostAsync(batchRequestContent); returnedResponse.Wait(-1); var response = returnedResponse.Result.GetResponseByIdAsync(counter.ToString()); response.Wait(-1); if (response.Result.IsSuccessStatusCode) { succesful.Add(contact.toGraphContact()); System.Console.WriteLine($"{contact.toGraphContact().Id} wurde hinzugefügt"); } else { failed.Add(contact.toGraphContact()); System.Console.WriteLine($"{contact.toGraphContact().Id} konnte nicht hinzugefügt werden"); } counter++; } }
序列化后的Contact JSON数据
{"AssistantName":null,"Birthday":null,"BusinessAddress":{"City":"Friesoythe","CountryOrRegion":"DE","PostalCode":"26169","State":"NIE","Street":"Zeppelinring 9","AdditionalData":null,"ODataType":null},"BusinessHomePage":"","BusinessPhones":null,"Children":null,"CompanyName":"Appel ","Department":"","DisplayName":"Thomas Appel","EmailAddresses":[{"Address":"tappel@stalleinrichtungen-appel.de","Name":"Thomas Appel (tappel@stalleinrichtungen-appel.de)","AdditionalData":null,"ODataType":null}],"FileAs":null,"Generation":null,"GivenName":"Thomas","HomeAddress":{"City":"Friesoythe","CountryOrRegion":"DE","PostalCode":"26169","State":"NIE","Street":"Zeppelinring 9","AdditionalData":null,"ODataType":null},"HomePhones":null,"ImAddresses":null,"Initials":null,"JobTitle":"","Manager":null,"MiddleName":null,"MobilePhone":null,"NickName":null,"OfficeLocation":"53,0191543°7,8857305","OtherAddress":null,"ParentFolderId":null,"PersonalNotes":null,"Profession":null,"SpouseName":null,"Surname":"Appel","Title":null,"YomiCompanyName":null,"YomiGivenName":null,"YomiSurname":null,"Extensions":null,"ExtensionsNextLink":null,"MultiValueExtendedProperties":null,"MultiValueExtendedPropertiesNextLink":null,"Photo":null,"SingleValueExtendedProperties":null,"SingleValueExtendedPropertiesNextLink":null,"Categories":["proALPHA_Export"],"ChangeKey":null,"CreatedDateTime":null,"LastModifiedDateTime":null,"Id":null,"ODataType":"microsoft.graph.contact","AdditionalData":null}
当前环境为O365租户,使用应用身份认证且权限已全部授予,请求帮忙排查问题。
排查方向
- 移除只读字段:请求体中的
CreatedDateTime、LastModifiedDateTime、Id、ChangeKey是Graph API自动生成的只读字段,提交这些字段会触发验证错误,创建联系人时不需要包含。 - 删除冗余的
ODataType字段:请求路径已经明确资源类型为联系人,请求体中无需指定ODataType,移除该字段可避免解析冲突。 - 修正批量请求的使用方式:当前循环中每次仅发送单个请求的批量调用是无效用法,正确做法是将多个请求(最多20个)打包到同一个
BatchRequestContent中一次性发送,减少请求次数并符合批量请求的设计逻辑。 - 处理空数组字段:
BusinessPhones、HomePhones等数组类型字段如果值为空,建议设置为空数组[]而非null,避免API解析异常。 - 打印错误响应详情:在
response.Result.IsSuccessStatusCode为false时,打印response.Result.Content获取具体的错误信息,这是定位400错误最直接的方式,比如字段格式错误、必填字段缺失等都会在响应中明确说明。
内容的提问来源于stack exchange,提问作者flam3shadow
相关产品推荐
相关产品推荐

