Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Using the O365 Android Connect sample code in your app

Ricardo Loo edited this page Jun 4, 2015 · 4 revisions

The goal of the Office 365 Connect sample for Android is to help you get your app connected to Office 365 data and services. In as little time as possible.

The main steps that you have to take to get your app connected are:

Copy gradle dependencies

Copy the following line to the build.gradle file of your app module.

// Azure Active Directory Library
compile 'com.microsoft.aad', name: 'adal', version: '1.1.2'

Include the Office 365 libraries dependencies that your app requires in your build.gradle file. You can specify the following dependencies:

  • Base OData: Include the odata-engine-core and odata-engine-android-impl. All apps that connect to Office 365 require these two dependencies.
  • Discovery: Include the discovery-services dependency below.
  • Mail, calendar, contacts (Exchange): Include outlook-services dependency.
  • Files (OneDrive for Business): Include the sharepoint-services dependency.
    // base OData library. Your app requires these two libraries
    // to use discovery, Outlook, or SharePoint services
    compile group: 'com.microsoft.services', name: 'odata-engine-core', version: '0.13.0'
    compile group: 'com.microsoft.services', name: 'odata-engine-android-impl', version: '0.13.0', ext:'aar'

    // choose the discovery services
    compile group: 'com.microsoft.services', name: 'discovery-services', version: '0.13.0'
	// choose the outlook services
    compile group: 'com.microsoft.services', name: 'outlook-services', version: '0.13.0'
	// choose the SharePoint services
    compile group: 'com.microsoft.services', name: 'sharepoint-services', version: '0.13.0'   

Request permissions in the app manifest

Your app requires the following permissions to use Office 365 client library. Copy these lines to the app manifest in your app module.

<!-- Required to connect to Office 365 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Required to use Outlook and SharePoint services -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Create a Constants interface

The Connect sample relies on a Java interface to store and retrieve information required to connect to Office 365. Create a Constants.java file with the following code:

interface Constants {
	public static final String AUTHORITY_URL = "https://login.microsoftonline.com/common";
    public static final String DISCOVERY_RESOURCE_URL = "https://api.office.com/discovery/v1.0/me/";
	public static final String DISCOVERY_RESOURCE_ID = "https://api.office.com/discovery/";
    public static final String MAIL_CAPABILITY = "Mail";
	// Update these two constants with the values for your application:
	public static final String CLIENT_ID = "<Your client id here>";
    public static final String REDIRECT_URI = "<Your redirect URI here>";
}

Make sure to update the CLIENT_ID and REDIRECT_URI variables with the values from your Azure app registration.

Copy the AuthenticationManager class to your project

Copy the AuthenticationManager class to your project. Make sure to update the namespace in the class to match your app module namespace.

Now you can use some code similar to the ConnectActivity class whenever you want to connect to Office 365.

AuthenticationManager.getInstance().setContextActivity(this);

AuthenticationManager.getInstance().connect(
        new AuthenticationCallback<AuthenticationResult>() {

            @Override
            public void onSuccess(AuthenticationResult result) {
                Log.i(TAG, "onConnectButtonClick - Successfully connected to Office 365");
            }

            @Override
            public void onError(final Exception e) {
                Log.e(TAG, "onCreate - " + e.getMessage());
            }
        });

Implement the onActivityResult method

In the activity where you want to connect to Office 365, implement the onActivityResult method. Here is some example code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(TAG, "onActivityResult - AuthenticationActivity has come back with results");
    super.onActivityResult(requestCode, resultCode, data);
    AuthenticationManager
            .getInstance()
            .getAuthenticationContext()
            .onActivityResult(requestCode, resultCode, data);
}

Get the discovery info for the service that you want to access

Copy the DiscoveryController class to your app module. Make sure to update the namespace to that of your module.

This class provides a helper method to obtain the Office 365 service information that you can use to create a client object. Here is some example code from the SendMailActivity class.

final SettableFuture<ServiceInfo> serviceDiscovered;

serviceDiscovered = DiscoveryController
        .getInstance()
        .getServiceInfo(Constants.MAIL_CAPABILITY);

Futures.addCallback(serviceDiscovered,
        new FutureCallback<ServiceInfo>() {
            @Override
            public void onSuccess(ServiceInfo serviceInfo) {
                Log.i(TAG, "onSendMailButtonClick - Mail service discovered");

            @Override
            public void onFailure(final Throwable t) {
                Log.e(TAG, "onSendMailButtonClick - " + t.getMessage());
            }
        });

Get and use a client object

Finally, you can create a client object. You can find the code that creates and uses the client object in the MailController class.

Here is the code that creates the client object:

AuthenticationManager.getInstance().setResourceId(mServiceResourceId);
ADALDependencyResolver dependencyResolver = (ADALDependencyResolver) AuthenticationManager
        .getInstance()
        .getDependencyResolver();

// You obtain the mServiceEndpointUri from the previous step, 
// get the discovery info for the service that you want to access.
OutlookClient mailClient = new OutlookClient(mServiceEndpointUri, dependencyResolver);

And here is how you can use the client object:

// Contact the Office 365 service and try to deliver the message.
ListenableFuture<Integer> mailSent = mailClient
        .getMe()
        .getOperations()
        .sendMail(messageToSend, true);
Futures.addCallback(mailSent,
        new FutureCallback<Integer>() {
            @Override
            public void onSuccess(Integer mailItemId) {
                Log.i(TAG, "sendMail - Email sent");
                result.set(true);
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "sendMail - " + t.getMessage());
                result.setException(t);
            }
        });