我正在尝试获取Azure AD中可用的用户。获取时,http 401 Authentication Error
遇到并IOException
在控制台上捕获。以下是我尝试获取数据的方法 -
public String getUsers(String accessToken) {
URL url;
String line = "";
try {
System.out.println("\n\n***************Available Users ************\n");
url = new URL("https://graph.windows.net/" + tenant_id
+ "/users");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Set the appropriate header fields in the request header.
con.setRequestMethod("GET");
con.setRequestProperty("api-version", "1.6");
con.setRequestProperty("Authorization", "Bearer " + accessToken);
con.setRequestProperty("Accept", "application/json;odata=minimalmetadata");
int responseCode = con.getResponseCode();
System.out.println("Responsecode : " + responseCode);
line=HttpClientHelper.getResponseStringFromConn(con,true);
System.out.println("Line data"+line);
/*if (responseCode == 200) {
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else {
reader = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
StringBuffer stringBuffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
stringBuffer.append(line);
}*/
System.out.println(line);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
答案 0 :(得分:0)
众所周知,您可以参考Error Handling in OAuth 2.0来检查您的问题。
与此同时,正如@GauravMantri所说,您还需要检查Microsoft Graph
在AAD申请的许可,请参阅下图。