Get started with the Microsoft Graph SDK for Java by integrating the Microsoft Graph API into your Java application!
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.
Add the repository and a compile dependency for microsoft-graph-auth
to your project's build.gradle
:
repository {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependency {
// Include the sdk as a dependency
compile('com.microsoft.graph:microsoft-graph-auth:0.3.0-SNAPSHOT')
}
Add the dependency in dependencies
in pom.xml
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph-auth</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
Add in project
<profiles>
<profile>
<id>allow-snapshots</id>
<activation><activeByDefault>true</activeByDefault></activation>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
Register your application by following the steps at Register your app with the Azure AD v2.0 endpoint.
AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(CLIENT_ID, SCOPES, AUTHORIZATION_CODE, REDIRECT_URL, NATIONAL_CLOUD, TENANT, CLIENT_SECRET);
ClientCredentialProvider authProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT_GUID, NATIONAL_CLOUD);
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(CLIENT_ID, SCOPES, USERNAME, PASSWORD, NATIONAL_CLOUD, TENANT, CLIENT_SECRET);
Using msgraph-sdk-java
IGraphServiceClient graphClient = GraphServiceClient
.builder()
.authenticationProvider(authProvider)
.buildClient();
User user = graphClient.me().buildRequest().get();
Using msgraph-sdk-java-core
OkHttpClient client = HttpClients.createDefault(authProvider);
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me").build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
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.
Using msgraph-sdk-java
IGraphServiceClient graphClient =
GraphServiceClient
.builder()
.authenticationProvider(authProvider)
.buildClient();
Drive drive = graphClient.me().drive().buildRequest().get();
Using msgraph-sdk-java-core
OkHttpClient httpclient = HttpClients.createDefault(authenticationProvider);
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me/drive").build();
Response response = client.newCall(request).execute();
System.out.println( respose.body().string() );
Steps to get authorizationCode
AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider("6731de76-14a6-49ae-97bc-6eba6914391e",
Arrays.asList("https://graph.microsoft.com/user.read","https://graph.microsoft.com/Mail.ReadWrite"),
authorizationCode,
"http://localhost/myapp/",
NationalCloud.Global,
"common",
"JqQX2PNo9bpM0uEihUPzyrh");
IGraphServiceClient graphClient =
GraphServiceClient
.builder()
.authenticationProvider(authProvider)
.buildClient();
IMessageCollectionPage page = graphClient.me().messages().buildRequest().get();
while(page != null) {
for(Message message : page.getCurrentPage()) {
System.out.println(message.subject);
}
IMessageCollectionRequestBuilder builder = page.getNextPage();
if(builder == null)break;
page = builder.buildRequest().get();
}
For more detailed documentation, see:
For known issues, see issues.
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!
The Microsoft Graph SDK for Java library is supported at runtime for Java 7+ and Android API revision 15 and greater.
Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.