使用有效的 JWT 令牌通过 Graph API 读取邮件时出现 403 禁止错误
403 Forbidden error when reading mails through Graph API with valid JWT token
我需要通过 Graph API 从 Outlook 邮箱中读取邮件。我正在编写的应用程序是一个没有用户交互的预定批处理作业。由于合规性原因,我无法使用应用程序权限。应用程序不能访问租户上的所有邮箱。我为共享允许邮箱的技术用户使用委派权限来实现这一点。我能够通过 ADAL4J 获得 JWT 访问令牌并成功调用了一些 APIs,但是每当我尝试读取邮箱甚至是技术用户邮箱时,我都会收到 403 禁止。
我从这个官方 [示例] (https://github.com/Azure-Samples/active-directory-java-native-headless/). After setting up my Application in Azure this sample worked right away. I then changed the Graph call to "https://graph.microsoft.com/v1.0/me/messages" 开始,突然收到 403 禁止访问。为避免权限问题,我将 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();
将 api 版本更改为测试版将解决此问题。
https://graph.microsoft.com/beta/me/messages
我需要通过 Graph API 从 Outlook 邮箱中读取邮件。我正在编写的应用程序是一个没有用户交互的预定批处理作业。由于合规性原因,我无法使用应用程序权限。应用程序不能访问租户上的所有邮箱。我为共享允许邮箱的技术用户使用委派权限来实现这一点。我能够通过 ADAL4J 获得 JWT 访问令牌并成功调用了一些 APIs,但是每当我尝试读取邮箱甚至是技术用户邮箱时,我都会收到 403 禁止。
我从这个官方 [示例] (https://github.com/Azure-Samples/active-directory-java-native-headless/). After setting up my Application in Azure this sample worked right away. I then changed the Graph call to "https://graph.microsoft.com/v1.0/me/messages" 开始,突然收到 403 禁止访问。为避免权限问题,我将 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();
将 api 版本更改为测试版将解决此问题。
https://graph.microsoft.com/beta/me/messages