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

如何将Graph API的GraphServiceException转为JSON格式并获取RequestID

使用Microsoft Graph API Java SDK发送邮件的异常处理与JSON转换需求

现有代码

我用以下Java代码调用Microsoft Graph API发送邮件:

_appClient.users(sFrom)
    .sendMail(UserSendMailParameterSet
        .newBuilder()
        .withMessage(message)
        .withSaveToSentItems(null)
        .build())
    .buildRequest()
    .post();

遇到的错误

调用时抛出如下异常:

com.microsoft.graph.http.GraphServiceException: Error code: ErrorAccessDenied
Error message: Access to OData is disabled.

POST https://graph.microsoft.com/v1.0/users/{some email ID name}/microsoft.graph.sendMail
SdkVersion : graph-java/v5.49.0
[...]

403 : Forbidden
[...]

[部分信息已截断,启用调试日志可查看更多详情]

需求

需要将该异常转换为如下格式的JSON,并提取其中的request-id:

{"error":{"code":"InvalidAuthenticationToken","message":"Access token is empty.","innerError":{"date":"2023-11-09T08:58:24","request-id":"cbe585bc-d83b-4611-9df8-f1e3ad9b74de","client-request-id":"cbe585bc-d83b-4611-9df8-f1e3ad9b74de"}}}

实现方法

步骤1:捕获并解析GraphServiceException

GraphServiceException内置了获取错误详情的API,可通过以下代码提取字段并转换为目标JSON格式:

try {
    // 执行发送邮件逻辑
    _appClient.users(sFrom)
        .sendMail(UserSendMailParameterSet
            .newBuilder()
            .withMessage(message)
            .withSaveToSentItems(null)
            .build())
        .buildRequest()
        .post();
} catch (GraphServiceException e) {
    ServiceError serviceError = e.getServiceError();
    if (serviceError != null) {
        // 构建innerError结构
        Map<String, Object> innerErrorMap = new HashMap<>();
        Map<String, Object> rawInnerError = serviceError.getInnerError();
        if (rawInnerError != null) {
            innerErrorMap.put("date", rawInnerError.get("date"));
            innerErrorMap.put("request-id", rawInnerError.get("request-id"));
            innerErrorMap.put("client-request-id", rawInnerError.get("client-request-id"));
        }

        // 构建完整错误结构
        Map<String, Object> errorDetailMap = new HashMap<>();
        errorDetailMap.put("code", serviceError.getCode());
        errorDetailMap.put("message", serviceError.getMessage());
        errorDetailMap.put("innerError", innerErrorMap);

        Map<String, Object> finalErrorMap = new HashMap<>();
        finalErrorMap.put("error", errorDetailMap);

        // 转换为JSON字符串(以Jackson为例)
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonResult = objectMapper.writeValueAsString(finalErrorMap);
        
        // 输出JSON结果
        System.out.println(jsonResult);

        // 单独提取request-id
        String requestId = rawInnerError != null ? (String) rawInnerError.get("request-id") : null;
        System.out.println("Request ID: " + requestId);
    }
}

步骤2:注意事项

  • 需确保项目引入JSON序列化库,比如Jackson的com.fasterxml.jackson.core:jackson-databind或Gson。
  • 对rawInnerError做判空处理,避免空指针异常。
  • 若需查看完整错误上下文,可在初始化Graph客户端时配置DEBUG级别的日志。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 17:17:42