【问题标题】:403 Forbidden error when reading mails through Graph API with valid JWT token使用有效 JWT 令牌通过 Graph API 读取邮件时出现 403 禁止错误
【发布时间】:2019-03-26 23:27:31
【问题描述】:

我需要通过 Graph API 从 Outlook 邮箱中读取邮件。我正在编写的应用程序是一个预定的批处理作业,没有用户交互。由于合规性原因,我无法使用应用程序权限。应用程序不能访问租户上的所有邮箱。我为共享允许邮箱的技术用户使用委派权限来实现这一目标。我能够通过 ADAL4J 获得 JWT 访问令牌并成功调用了一些 API,但是每当我尝试读取邮箱时,即使是技术用户邮箱,我都会收到 403 禁止。

我从这个官方 [示例] (https://github.com/Azure-Samples/active-directory-java-native-headless/) 开始。在 Azure 中设置我的应用程序后,此示例立即生效。然后我将 Graph 调用更改为“https://graph.microsoft.com/v1.0/me/messages”,突然我收到了 403 Forbidden。为了避免权限问题,我将 Azure AD 中可用的所有委派权限添加到应用程序,并为所有内容提供了管理员同意。不幸的是,这并没有改变什么。当我检查令牌的内容时,我看到 scp 字段包含所有权限。奇怪的是,我居然可以写信箱。我可以通过 Graph API 写入草稿文件夹。但是,当我获取返回的消息 ID 并尝试查询我刚刚创建的同一条消息时,我再次收到 403 Forbidden。

获取令牌

private static AuthenticationResult getAccessTokenFromUserCredentials(
            String username, String password) throws Exception {
        AuthenticationContext context;
        AuthenticationResult result;
        ExecutorService service = null;
        try {
            service = Executors.newFixedThreadPool(1);
            context = new AuthenticationContext(AUTHORITY, false, service);
            Future<AuthenticationResult> future = context.acquireToken(
                    "https://graph.microsoft.com", CLIENT_ID, username, password,
                    null);
            result = future.get();
        } finally {
            service.shutdown();
        }
 return result;
}

调用消息端点:

        URL url = new URL("https://graph.microsoft.com/v1.0/me/messages");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", "Bearer " + accessToken);
        conn.setRequestProperty("Accept","application/json");
        int httpResponseCode = conn.getResponseCode();

【问题讨论】:

    标签: java outlook azure-ad-graph-api adal4j


    【解决方案1】:

    将 api 版本更改为 beta 将解决此问题。

    https://graph.microsoft.com/beta/me/messages
    

    【讨论】:

    • 谢谢。它解决了我的问题,但附带了一个新问题。测试版可能随时更改,因此我无法将其放入生产环境。您是否知道哪些测试版更改会改变这种行为,以及在 v1.0 上是否有解决方法?
    • 不客气。我想这可能是一个错误。您可以在 o365 门户上提出支持请求以获得进一步的帮助。 docs.microsoft.com/en-us/office365/admin/…
    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 2021-12-26
    • 2022-07-15
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多