403使用有效的JWT令牌通过Graph API读取邮件时出现禁止错误

问题描述 投票:0回答:1

我需要通过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个回答
0
投票

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

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.