我通过Microsoft Graph API访问我的Office 365帐户时遇到问题(或两个问题)。通过图形API访问Office 365
第一个问题是我有一个Java程序试图列出Office 365订阅中的所有用户。我打电话给https://graph.microsoft.com/v1.0/users/
,但得到403 forbidden
。
在应用程序注册上,我已添加包括User.Read
,User.ReadBasic.All
,User.ReadWrite
在内的权限,同时授权和应用程序权限。
我也尝试过使用Graph Explorer,但是当我输入使用我的帐户时,它仍然使用内置的图形用户,并且不显示我的应用程序登录信息。不确定这些是否相关。
下面是代码片段,结果在一个403
AuthenticationResult result = getAccessTokenFromUserCredentials(RESOURCE_GRAPH, ID, PASSWORD);
URL url = new URL("https://graph.microsoft.com/v1.0/users/") ;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer "+result.getAccessToken());
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
这里是获取令牌
private static AuthenticationResult getAccessTokenFromUserCredentials(String resource,
String username, String password) throws Exception {
AuthenticationContext context;
AuthenticationResult result = null;
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(AUTHORITY, false, service);
Future<AuthenticationResult> future = context.acquireToken(
resource, CLIENT_ID, username, password,
null);
result = future.get();
} finally {
service.shutdown();
}
if (result == null) {
throw new ServiceUnavailableException(
"authentication result was null");
}
return result;
}
您如何验证用户以获取您传入'/ users'的令牌? –
已更新,代码示例。如果我调用https://graph.microsoft.com/v1.0/me/,则返回代表我的用户的json字符串。但如果我打电话给/ users /我得到了403.我也可以访问sharepoint终点ok并获取网站数据等。 – Gary
User.ReadBasic.All需要管理员同意,你有没有试过做管理员同意?你在哪里注册的应用程序,在天蓝色的门户? –