Use MS Graph Android Authentication library to authenticate to use Microsoft Graph API in your Android application! Use this authentication library along with msgraph-sdk-java or msgraph-sdk-java-core to make requests to Microsoft Graph APIs
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.
allprojects {
repositories {
google()
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
}
dependency {
// Include the auth library as a dependency in app level build.gradle
implementation 'com.microsoft.graph:microsoft-graph-android-auth:0.1.0-SNAPSHOT'
}
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Step 4 : Add BrowserTabActivity activity in AndroidManifest.xml. Make sure to put your App/Client ID at <YOUR_CLIENT_ID>.
<!--Intent filter to capture System Browser calling back to our app after Sign In-->
<activity
android:name="com.microsoft.identity.client.BrowserTabActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="msal<YOUR_CLIENT_ID>"
android:host="auth" />
</intent-filter>
</activity>
Register your application by following the steps at Register your app with the Azure AD v2.0 endpoint. Once registered you will get CLIENT_ID of your application.
PublicClientApplication publicClientApplication = new PublicClientApplication(getApplicationContext(), "CLIENT_ID_OF_YOUR_APPLICATION");
MSALAuthenticationProvider msalAuthenticationProvider = new MSALAuthenticationProvider(
getActivity(),
getApplication(),
publicClientApplication,
scopes);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
msalAuthenticationProvider.handleInteractiveRequestRedirect(requestCode, resultCode, data);
}
2.4 Make requests against the Microsoft Graph REST API
Usage in msgraph-sdk-java
IGraphServiceClient graphClient =
GraphServiceClient
.builder()
.authenticationProvider(msalAuthenticationProvider)
.buildClient();
User user = graphClient
.me()
.buildRequest()
.get();
System.out.println(user.displayName);
Usage in msgraph-sdk-java-core
OkHttpClient graphClient = HttpClients.createDefault(msalAuthenticationProvider);
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());
Usage in msgraph-sdk-java
IGraphServiceClient graphClient =
GraphServiceClient
.builder()
.authenticationProvider(msalAuthenticationProvider)
.buildClient();
graphClient
.me()
.drive()
.buildRequest()
.get(new ICallback<Drive>() {
@Override
public void success(final Drive result) {
System.out.println("Found Drive " + result.id);
}
...
// Handle failure case
});
Usage in msgraph-sdk-java-core
OkHttpClient graphClient = HttpClients.createDefault(msalAuthenticationProvider);
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me/drive").build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
For known issues, see issues.
This library 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!
This library supports runtime for Java 7+ and Android API revision 21 and greater.
Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.