You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Microsoft Graph API更新联系人时遇UnableToDeserializePostBody错误

问题:为联系人添加Schema Extension时返回UnableToDeserializePostBody错误

使用Java版Microsoft Graph SDK调用Microsoft Graph API,尝试为新联系人添加Schema Extension时,请求始终返回UnableToDeserializePostBody错误。

相关代码

检查并创建Schema Extension的代码

SchemaExtensionCollectionPage response = graphClient.schemaExtensions().buildRequest().get();
List<SchemaExtension> extensions = fetchAll(response);
contactExtension = extensions.stream()
    .filter(e-> (contactExtensionId).equals(e.id)) //contactExtensionId fetched from internal DB
    .findAny().orElse(null);

if(contactExtension == null)
{
    contactExtension = new SchemaExtension();
    contactExtension.id = contactExtensionId;
    contactExtension.description = "Contact extension";
    LinkedList<String> targetTypesList = new LinkedList<>();
    targetTypesList.add("contact");
    contactExtension.targetTypes = targetTypesList;
    
    LinkedList<ExtensionSchemaProperty> propertiesList = new LinkedList<>();
    ExtensionSchemaProperty tenantProperty = new ExtensionSchemaProperty();
    tenantProperty.name = EXTENSION_PROPERTY_TENANT;
    tenantProperty.type = "String";
    propertiesList.add(tenantProperty);
    ExtensionSchemaProperty idProperty = new ExtensionSchemaProperty();
    idProperty.name = EXTENSION_PROPERTY_ID;
    idProperty.type = "String";
    propertiesList.add(idProperty);
    contactExtension.properties = propertiesList;
    
    contactExtension = graphClient.schemaExtensions()
            .buildRequest()
            .post(contactExtension);
            
    logger.info("Contact Extension created : " + contactExtension.id);
}

联系人扩展类定义

public static class ContactExtension extends Extension
{
    @SerializedName(value = "tenant", alternate = {"Tenant"})
    @Expose
    @Nullable
    public String tenant;
    
    @SerializedName(value = "entityId", alternate = {"EntityId"})
    @Expose
    @Nullable
    public String entityId;
}

联系人更新请求代码

Extension ext = graphClient.users(targetUser.id)
    .contacts(contact.id)
    .extensions(this.contactExtension.id)
    .buildRequest()
    .post(contactExt);

错误堆栈

Jul 12, 2023 1:53:03 PM com.microsoft.graph.logger.DefaultLogger logError
    SEVERE: CoreHttpProvider[sendRequestInternal] - 408Graph service exception
    Jul 12, 2023 1:53:03 PM com.microsoft.graph.logger.DefaultLogger logError
    SEVERE: Throwable detail: com.microsoft.graph.http.GraphServiceException: Error code: UnableToDeserializePostBody
    Error message: were unable to deserialize

    POST https://graph.microsoft.com/v1.0/users/{user}/contacts/{contacts}/extensions/extdtlnlmgg_contactMetaData
    SdkVersion : graph-java/v5.49.0
        [...]

        400 : Bad Request
[...]

        [Some information was truncated for brevity, enable debug logging for more details]
    at com.microsoft.graph.http.GraphServiceException.createFromResponse(GraphServiceException.java:419)
    at com.microsoft.graph.http.GraphServiceException.createFromResponse(GraphServiceException.java:378)
    at com.microsoft.graph.http.CoreHttpProvider.handleErrorResponse(CoreHttpProvider.java:512)
    at com.microsoft.graph.http.CoreHttpProvider.processResponse(CoreHttpProvider.java:442)
    at com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:408)
    at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:225)
    at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:202)
    at com.microsoft.graph.http.BaseRequest.send(BaseRequest.java:335)

问题根源与修复方案

1. 请求端点路径错误

当前请求的路径是/users/{user}/contacts/{contacts}/extensions/{extension-id}并使用POST方法,不符合Graph API规范:

  • 为联系人创建Schema Extension实例,应POST到/users/{user-id}/contacts/{contact-id}/extensions集合端点,而非指定单个扩展ID的端点。指定ID的POST通常用于更新操作,且需使用PUT方法而非POST。

修复后的请求代码:

Extension ext = graphClient.users(targetUser.id)
    .contacts(contact.id)
    .extensions() // 直接操作extensions集合,不指定扩展ID
    .buildRequest()
    .post(contactExt);

2. 扩展类缺少@odata.type注解

Graph API需要请求体包含@odata.type字段识别扩展类型,当前ContactExtension类未添加该注解,导致序列化后的JSON缺少关键标识,API无法解析。

修改扩展类添加注解:

public static class ContactExtension extends Extension
{
    @SerializedName("@odata.type")
    @Expose
    private String odataType = "#microsoft.graph.schemaExtension"; // 指定扩展类型
    
    @SerializedName(value = "tenant", alternate = {"Tenant"})
    @Expose
    @Nullable
    public String tenant;
    
    @SerializedName(value = "entityId", alternate = {"EntityId"})
    @Expose
    @Nullable
    public String entityId;
}

3. 属性名匹配检查

确保ContactExtension类的字段注解名称,与创建Schema Extension时定义的properties名称完全一致:

  • 核对EXTENSION_PROPERTY_TENANT值是否为"tenant",EXTENSION_PROPERTY_ID值是否为"entityId",若不匹配需同步修改注解或Schema Extension的属性定义。

4. 开启Debug日志排查请求体

启用SDK的Debug日志,查看实际发送的JSON请求体,确认是否存在格式错误、缺失字段等问题:

// 初始化GraphClient时配置Debug日志
DefaultLogger logger = new DefaultLogger();
logger.setLoggingLevel(LoggerLevel.DEBUG);
GraphServiceClient<Request> graphClient = GraphServiceClient.builder()
    .authenticationProvider(authProvider)
    .logger(logger)
    .buildClient();

通过日志中的请求体内容,可直接定位API无法反序列化的具体原因。

内容的提问来源于stack exchange,提问作者Fradven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 19:50:27