6

I am trying to access Microsoft Graph API using the following code in Java :-

    String url_str = "https://graph.microsoft.com/v1.0/users/";
    String access_token = getAccessToken();
    url = new URL(url_str);
    con = ( HttpURLConnection )url.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestMethod("GET");
    con.setRequestProperty("Authorization", access_token);
    con.setRequestProperty("Accept","application/json");
    con.connect();

    br = new BufferedReader(new InputStreamReader( con.getInputStream() ));
    String str = null;
    String line;
    while((line = br.readLine()) != null) {
        str += line;
    }
    System.out.println(str);
} catch (Exception e) {
    e.printStackTrace();
}

Currently I am getting JSON String which i will need to parse further. All I want to know is there any other way which will reduce the pain of deserialization or something more better.

1

4 Answers 4

5

Update on Martin answer, Java SDK is released and available for public preview :- https://github.com/microsoftgraph/msgraph-sdk-java.

Sign up to request clarification or add additional context in comments.

Comments

2

Im not aware of any Java SDK for Microsoft Graph. However, you can use jsonutil to generate your java objects which will reduce at least some work.

Edit: As mentioned by Pranay, a Java SDK is released, you should use this instead.

1 Comment

This is no longer the best advice - use the Java SDK as noted in the other answers instead.
2

Another Java client for Microsoft Graph is odata-client which has support for the Graph v1.0, Beta, Graph Explorer endpoints and for Dynamics CRM and Analytics for Devops OData services. I was an early collaborator on the msgraph-sdk-java library and the problems there led me to create what I think is a much stronger product. In particular odata-client-msgraph (and the sibling libraries)

  • uses immutability
  • detects changes for patching
  • models inheritance
  • extensively uses fluent builders
  • implements java.util.Stream on collections
  • provides easier authentication
  • offers better type safety
  • has very clean generated code
  • has better discoverability
  • has decent unit test coverage of API interactions
  • offers email helpers for that part of the API
  • is released very frequently in line with frequent service updates

Comments

0

You should use the newly-released Java SDK for Microsoft Graph. More details were recently posted here, and the github repo for the SDK is here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.