Skip to content
Authentication Providers for Microsoft Graph Java SDK
Branch: dev
Clone or download
Latest commit 0e94039 Apr 17, 2019

Microsoft Graph Auth Preview SDK for Java

Get started with the Microsoft Graph SDK for Java by integrating the Microsoft Graph API into your Java application!

Important Note about the Microsoft Graph Auth Preview SDK for Java

During the preview we may make changes to the API, and other mechanisms of this library, which you will be required to take along with bug fixes or feature improvements. This may impact your application. An API change may require you to update your code. When we provide the General Availability release we will require you to update to the General Availability version within six months, as applications written using a preview version of library may no longer work.

1. Installation

1.1 Install via Gradle

Add the repository and a compile dependency for microsoft-graph-auth to your project's build.gradle:

repository {
        url ''

dependency {
    // Include the sdk as a dependency

1.2 Install via Maven

Add the dependency in dependencies in pom.xml


Add in project


2. Getting started

2.1 Register your application

Register your application by following the steps at Register your app with the Azure AD v2.0 endpoint.

2.2 Create an authentication provider object

2.3.1 Confidential client authentication provider

a. Authorization code provider
AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(CLIENT_ID, SCOPES, AUTHORIZATION_CODE, REDIRECT_URL, NATIONAL_CLOUD, TENANT, CLIENT_SECRET);
b. Client credential provider
ClientCredentialProvider authProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT_GUID, NATIONAL_CLOUD);

2.3.2 Public client authentication provider

a. Username password provider
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(CLIENT_ID, SCOPES, USERNAME, PASSWORD, NATIONAL_CLOUD, TENANT, CLIENT_SECRET);

2.3 Get a HttpClient object and make a call

Using msgraph-sdk-java

IGraphServiceClient graphClient = GraphServiceClient

User user =;

Using msgraph-sdk-java-core

OkHttpClient client = HttpClients.createDefault(authProvider);
Request request = new Request.Builder().url("").build();
Response response = client.newCall(request).execute();

3. Make requests against the service

After you have a GraphServiceClient that is authenticated, you can begin making calls against the service. The requests against the service look like our REST API.

3.1 Get the user's drive

To retrieve the user's drive:

Using msgraph-sdk-java
IGraphServiceClient graphClient = 
Drive drive =;
Using msgraph-sdk-java-core
OkHttpClient httpclient = HttpClients.createDefault(authenticationProvider);
Request request = new Request.Builder().url("").build();
Response response = client.newCall(request).execute();
System.out.println( respose.body().string() );

4. Sample

4.1 Authorization code provider

Steps to get authorizationCode

AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider("6731de76-14a6-49ae-97bc-6eba6914391e", 
IGraphServiceClient graphClient = 
IMessageCollectionPage page =;
while(page != null) {
	for(Message message : page.getCurrentPage()) {
	IMessageCollectionRequestBuilder builder = page.getNextPage();
	if(builder == null)break;
	page = builder.buildRequest().get();

5. Documentation

For more detailed documentation, see:

Usage of these providers.

6. Issues

For known issues, see issues.

7. Contributions

The Microsoft Graph SDK is open for contribution. To contribute to this project, see Contributing.

Thanks to everyone who has already devoted time to improving the library:

Nakul Sabharwal

Deepak Agrawal

This project follows the all-contributors specification. Contributions of any kind are welcome!

8. Supported Java versions

The Microsoft Graph SDK for Java library is supported at runtime for Java 7+ and Android API revision 15 and greater.

9. License

Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.

10. Third-party notices

Third-party notices

You can’t perform that action at this time.