我通過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需要管理員同意,你有沒有試過做管理員同意?你在哪裏註冊的應用程序,在天藍色的門戶? –