Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我试图从java代码中使用Microsoft Graph api从Azure active directory检索用户信息,但收到400错误(错误请求)_Java_Http_Microsoft Graph Api_Httpurlconnection_Azure Ad Graph Api - Fatal编程技术网

我试图从java代码中使用Microsoft Graph api从Azure active directory检索用户信息,但收到400错误(错误请求)

我试图从java代码中使用Microsoft Graph api从Azure active directory检索用户信息,但收到400错误(错误请求),java,http,microsoft-graph-api,httpurlconnection,azure-ad-graph-api,Java,Http,Microsoft Graph Api,Httpurlconnection,Azure Ad Graph Api,我试图从java代码中使用Microsoft Graph api从Azure active directory检索用户信息,但我收到400个错误(错误请求),但它在postman中运行良好 我正在使用的java代码是 String url = "https://graph.microsoft.com/v1.0/users?$filter=displayName eq 'Dasari Siri'"; String token = "Bearer "+accesstoken;

我试图从java代码中使用Microsoft Graph api从Azure active directory检索用户信息,但我收到400个错误(错误请求),但它在postman中运行良好

我正在使用的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)

请你把这件事讲一遍,告诉我哪里出了错,好吗


提前感谢

您需要对url的过滤器部分进行编码,并添加一行将属性“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();
              }
        }
    }

}

请您对url的过滤部分进行编码,然后再试一次好吗?@HuryShen谢谢您的建议,我对此非常陌生。您可以让我知道如何使用shortHi进行此操作吗?请参考我在下面提供的解决方案。如果这有助于解决您的问题,请将其作为答案(单击我答案旁边的复选标记,将其从灰色切换为填充),提前感谢~Thank you@HuryShen它正在工作,但我有一个小问题,希望您能帮助我。问题是我正在获取所有用户的信息,与仅获取指定用户的详细信息不同,url应该更改为
stringURL=”https://graph.microsoft.com/v1.0/users$filter=“+urlcoder.encode”(“displayName eq'huryTest'”,“UTF-8”)