【发布时间】:2020-08-07 17:38:11
【问题描述】:
我正在尝试使用 Java 代码中的 Microsoft Graph api 从 Azure 活动目录中检索用户信息,但我收到 400 错误(错误请求),但它在邮递员中运行良好。
我正在使用的java代码是
String url = "https://graph.microsoft.com/v1.0/users?$filter=displayName eq 'Dasari Siri'";
String token = "Bearer "+accesstoken;
URL obj = new URL(url);
HttpURLConnection con =(HttpURLConnection)obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", token);
con.connect();
StringBuffer Response = new StringBuffer();
if(con!=null){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String input ;
while ((input = br.readLine()) != null){
Response.append(input);
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
我得到的错误是
java.io.IOException: Server returned HTTP response code: 400 for URL: https://graph.microsoft.com/v1.0/users?$filter=displayName eq 'Dasari Siri'
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at com.javatpoint.Downloadcsv.doDownloadCsv(Downloadcsv.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
The postman request which worked fine for me is :
请你看一下,让我知道我在哪里犯了错误
提前谢谢你
【问题讨论】:
-
能否请您对 url 的过滤器部分进行编码,然后重试?
-
@HuryShen 谢谢你的建议,我对这个很陌生,你能告诉我怎么做吗
-
您好,请参考我下面提供的解决方案。如果对您的问题有帮助,请accept作为答案(点击我的答案旁边的复选标记,将其从灰色切换为已填充),在此先感谢~
标签: java http microsoft-graph-api httpurlconnection azure-ad-graph-api