[我正尝试从Java代码中使用Microsoft Graph api从Azure活动目录中检索用户信息,但出现400错误(错误请求)

问题描述 投票:1回答:1

[我试图使用Microsoft Graph api从Java代码从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 :

[您能告诉我我在哪里犯错吗

提前谢谢您

java http microsoft-graph httpurlconnection azure-ad-graph-api
1个回答
1
投票

您需要对网址的过滤器部分进行编码,并添加一行以将属性“ Accept”设置为“ application / json”)。请参考下面的代码:

public class Testgraph {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        String url = "https://graph.microsoft.com/v1.0/users?" + URLEncoder.encode("$filter=displayName eq 'huryTest'", "UTF-8");
        String token = "Bearer " + "your access token"; 
        URL obj = new URL(url);

        HttpURLConnection con =(HttpURLConnection)obj.openConnection();  
        con.setRequestMethod("GET");
        con.setRequestProperty("Authorization", token);
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Content-Type", "application/json");
        int responseCode = con.getResponseCode();

        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);
                 System.out.print("---------success------");
               }
               br.close();
                   }              
               catch (IOException e) {
               e.printStackTrace();
            }catch(Exception e) {
                 e.printStackTrace();
              }
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.