Posted by on May 24, 2016 in #AndroidDev, #Office365Dev | 0 comments

There is an easy way to call a great amount of Microsoft APIs – using one single endpoint. This endpoint, so called the Microsoft Graph (https://graph.microsoft.io/) lets you access everything from data, to intelligence and insights powered by the Microsoft cloud.

No longer will you need to keep track of different endpoints and separate tokens in your solutions – how great is that? This post is an introductory part of getting started with the Microsoft Graph. For changes in the Microsoft Graph, head to: https://graph.microsoft.io/changelog

This blog post demonstrated how to leverage the Microsoft Graph SDK for Android, in your application project. Before you can get started, be sure to go through this blog post on how to get started with ADAL (Active Directory Authentication Library) for Android: http://simonjaeger.com/get-started-with-adal-for-android/ 

Learn more about ADAL here: https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-libraries/

Calling the operations on the Microsoft Graph is super simple, and in this code sample we will simply get the user profile. After that you’ll be all set, and everything will be up to your imagination!

Throughout this blog post I will be using Android Studio, which is something you can grab for free here: https://developer.android.com/studio/install.html

You will also need to register your application (Native Client Application) in Azure AD to follow along. You can learn more about doing this here:

Add the SDK

The first thing we need to do is to get the Microsoft Graph SDK into our project. This can be done in many different ways (cloning the library from GitHub, Maven, Gradle, etc.) and if you already know your way around your tools – get it! If not, no worries, we will go through it using Gradle.

  • In the Project window, find and expand the Gradle Scripts section.
  • Get into the build.gradle module file. Noted by the “(Module: app)” part.
    android2
  • Add the Microsoft Graph SDK dependency inside the dependencies section:
  • Add the Google-Gson dependency inside the dependencies section:
  • Set the minSdkVersion to at least 15, this is found in the defaultConfig section:
  • Your build.gradle file should now look something like this:
  • Finally you need to run the Gradle Scripts, you do this by hitting the Sync Now button that shows up in the top right corner.
    android3

This will get the Microsoft Graph SDK for Android downloaded and added to your project.

Code Structure

Assuming you have sorted out your ADAL dependency and got it in place (http://simonjaeger.com/get-started-with-adal-for-android/) – we can begin. The theory of how the Microsoft Graph SDK works is pretty simple.

  • Get your authentication logic in place (preferably with ADAL).
  • Instantiate an IGraphServiceClient object.
  • Feed a client configuration (IClientConfig) containing an IAuthenticationProvider.
  • The IAuthenticationProvider serves as a middle layer between the IGraphServiceClient object and the REST endpoint. Its purpose is to attach any outgoing requests with a valid Authorization header.
  • Start calling the Microsoft Graph!

So let’s code this.

  • Add a few constant strings to help us with needed keywords:
  • Define an IClientConfig object with the IAuthenticationProvider that adds the access token to the outgoing request object (IHttpRequest):
  • Create the IGraphServiceClient object:
  • Call the Microsoft Graph. This is done by reaching the desired enitity or collection (defined in the client service object), building the request object of it and finally specifying the HTTP method.
  • At this point, my entire Activity looked like this:

Please note that the code snippet above is merely a proof of concept of an implementation of ADAL. Once you have grasped the library – I would encourage you to implement a better pattern for acquiring the tokens. It’s very straight forward, but it creates a better user experience:

  • Create your AuthenticationContext with an ITokenCacheStore object (e.g. DefaultTokenCacheStore, FileTokenCacheStore, MemoryTokenCacheStore, or your own implementation).
  • Call the acquireTokenSilent method.
  • Validate the response (AuthenticationResult). If it failed, now call the acquireToken method and show UI.
  • Now use the acquired tokens as needed.

Now run your application by clicking on the Debug button (you can click Run in the menu and click on the Debug ‘app’ button). Your app will start, prompt you to sign in and finally display the tokens in the Android Monitor.

msgraphsdk2

-Simon Jaeger