我正在尝试访问特定用户的托管设备。我编写了一个使用 Web 应用程序获取身份验证代码的代码。我能够看到所有用户以及特定用户。但是,当我尝试为用户访问托管设备时,我收到 401 未经授权的错误。我已检查授予在 Microsoft Graph 的 Azure 门户中创建的 Web 应用程序的所有权限。这是我的代码:-
try {
String access_token = getAccessToken();
String url_str = "https://graph.microsoft.com/v1.0/users/{user name here}/managedDevices/";
url = new URL(url_str);
con = ( HttpURLConnection )url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", access_token);
con.setRequestProperty("Accept","application/json");
con.connect();
br = new BufferedReader(new InputStreamReader( con.getInputStream() ));
String str = null;
String line;
while((line = br.readLine()) != null) {
str += line;
}
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
令牌检索代码:-
private String getAccessToken() {
String accessToken = "";
try {
ExecutorService service = Executors.newFixedThreadPool(1);
String authorization_url = "https://login.microsoftonline.com/" + Authentication_Constants.TENANT + "/oauth2/authorize/";
AuthenticationContext authContext = new AuthenticationContext(authorization_url, false, service);
ClientCredential clientCred = new ClientCredential(Authentication_Constants.CLIENTID, Authentication_Constants.SECRET);
Future<AuthenticationResult> future = authContext.acquireToken(Authentication_Constants.RESOURCE, clientCred, null);
AuthenticationResult authResult = future.get();
accessToken = authResult.getAccessToken();
} catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
}
return accessToken;
}
有什么我想念的吗?谢谢!