【发布时间】: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