Search code examples
javahttpmicrosoft-graph-apihttpurlconnectionazure-ad-graph-api

I am trying to retrieve users information from Azure active directory using Microsoft Graph api from java code but i am getting 400 error(Bad request)


I am trying to retrieve users information from Azure active directory using Microsoft Graph api from java code but i am getting 400 error(Bad request) , but it works well in postman .

The java code with which i am working is

    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();
          }
}

The error which I am getting is

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 :

Can you please go through this and let me know where i am making mistake

Thank you in advance


Solution

  • You need to do the encode for the filter part of the url and add a line to set property "Accept" as "application/json"). Please refer to my code below:

    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();
                  }
            }
        }
    
    }