OfficeDev/Outlook-SDK-Android

Name: Outlook-SDK-Android

Owner: Office Developer

Description: Build apps for Outlook, Outlook.com, and Office 365 users with one set of APIs.

Created: 2015-11-16 21:47:50.0

Updated: 2018-01-22 18:46:34.0

Pushed: 2017-12-28 21:35:40.0

Homepage: https://dev.outlook.com/

Size: 370

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Outlook API SDK for Android

Important: This preview SDK has been deprecated and is no longer being maintained. We recommend that you use Microsoft Graph and the associated Microsoft Graph SDKs instead.

Build apps for Outlook, Outlook.com, and Office 365 users with one set of APIs.


:exclamation:NOTE: You are free to use this code and library according to the terms of its included LICENSE and to open issues in this repo for unofficial support.

Information about official Microsoft support is available here.


These libraries are generated from API metadata using Vipr and Vipr-T4TemplateWriter and use a shared client stack provided by orc-for-android.

For information on release cadence and how to access built binaries before release, see Releases.

Quick Start

To use this library in your project, follow these general steps, as described further below:

  1. Configure dependencies in build.gradle.
  2. Set up authentication.
  3. Construct an API client.
  4. Call methods to make REST calls and receive results.
Setup
  1. From the Android Studio splash screen, click “Start a new Android Studio project”. Name your application as you wish.

  2. Select “Phone and Tablet” and set Minimum SDK as API 18, then click Next. Choose “Blank Activity”, then click Next. The defaults are fine for the activity name, so click Finish.

  3. Open the Project view in the left-hand column if it's not open. From the list of Gradle Scripts, find the one title “build.gradle (Module: app)” and double-click to open it.

  4. In the dependencies closure, add the following dependencies to the compile configuration: if using the current registration portal (Azure) :

    ile('com.microsoft.services:outlook-services:2.0.0'){
    transitive = true
    
    

    or if using the new Application Registation Portal :

    ile('com.microsoft.services:outlook-services:2.1.0'){
    transitive = true
    
    

    You may want to click the “Sync Project with Gradle Files” button in the toolbar. This will download the dependencies so Android Studio can assist in coding with them.

  5. Find AndroidManifest.xml and add the following line within the manifest section:

    es-permission android:name="android.permission.INTERNET" />
    
Authenticate and construct client

With your project prepared, the next step is to initialize the dependency manager and an API client.

:exclamation: If you haven't yet registered your app in Azure AD, you'll need to do so before completing this step by following these instructions.

:exclamation: If you haven't yet registered the Application Registation Portal, you'll need to do so before completing this step by following these instructions.

  1. From the Project view in Android Studio, find app/src/main/res/values, right-click it, and choose New > Values resource file. Name your file adal_settings.

  2. Fill in the file with values from your app registration, as in the following example. Be sure to paste in your app registration values for the Client ID and Redirect URL.

    ing name="AADAuthority">https://login.microsoftonline.com/common</string>
    ing name="AADResourceId">https://outlook.office.com</string>
    ing name="AADClientId">Paste your Client ID HERE</string>
    ing name="AADRedirectUrl">Paste your Redirect URI HERE</string>
    
  3. Add an id to the “Hello World” TextView. Open app/src/main/res/layout/activity_main.xml. Use the following tag.

    oid:id="@+id/messages"
    
  4. Set up the DependencyResolver

    Open the MainActivity class and add the following imports:

    rt com.google.common.util.concurrent.FutureCallback;
    rt com.google.common.util.concurrent.Futures;
    rt com.google.common.util.concurrent.SettableFuture;
    rt com.microsoft.aad.adal.AuthenticationCallback;
    rt com.microsoft.aad.adal.AuthenticationContext;
    rt com.microsoft.aad.adal.AuthenticationResult;
    rt com.microsoft.aad.adal.PromptBehavior;
    rt com.microsoft.services.outlook.*;
    rt com.microsoft.services.outlook.fetchers.OutlookClient;    
    rt static com.microsoft.aad.adal.AuthenticationResult.AuthenticationStatus;
    

    Then, add these instance fields to the MainActivity class:

    ate AuthenticationContext mAuthContext;
    ate DependencyResolver mResolver;
    ate TextView messagesTextView;
    ate String[] scopes = new String[]{"http://outlook.office.com/Mail.Read"};
    

    Add the following method to the MainActivity class. The logon() method constructs and initializes ADAL's AuthenticationContext, carries out interactive logon, and constructs the DependencyResolver using the ready-to-use AuthenticationContext.

    ected SettableFuture<Boolean> logon() {
    final SettableFuture<Boolean> result = SettableFuture.create();
    
    try {
        mAuthContext = new AuthenticationContext(this, getString(R.string.AADAuthority), true);
        mAuthContext.acquireToken(
                this,
                scopes,
                null,
                getString(R.string.AADClientId),
                getString(R.string.AADRedirectUrl),
                PromptBehavior.Auto,
                new AuthenticationCallback<AuthenticationResult>() {
    
                    @Override
                    public void onSuccess(final AuthenticationResult authenticationResult) {
                        if (authenticationResult != null
                                && authenticationResult.getStatus() == AuthenticationStatus.Succeeded) {
                            mResolver = new DependencyResolver.Builder(
                                            new OkHttpTransport(), new GsonSerializer(),
                                            new AuthenticationCredentials() {
                                            @Override
                                            public Credentials getCredentials() {
                                                return new OAuthCredentials(token);
                                            }
                                        }).build();
                            result.set(true);
                        }
                    }
    
                    @Override
                    public void onError(Exception e) {
                        result.setException(e);
                    }
    
                });
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        e.printStackTrace();
        result.setException(e);
    }
    return result;
    
    

    You also must configure MainActivity to pass the result of authentication back to the AuthenticationContext by adding this method to its class:

    rride
    ected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mAuthContext.onActivityResult(requestCode, resultCode, data);
    
    

    From MainActivity.onCreate, cache the messages TextView, then call logon() and hook up to its completion using the following code:

    essagesTextView = (TextView) findViewById(R.id.messages);
    utures.addCallback(logon(), new FutureCallback<Boolean>() {
        @Override
        public void onSuccess(Boolean result) {
    
        }
    
        @Override
        public void onFailure(Throwable t) {
            Log.e("logon", t.getMessage());
        }
    });
    
  5. Now, add the necessary code to create an API client.

    Add a private static variable with the Outlook base URL:

    ate static final String outlookBaseUrl = "https://outlook.office.com/api/v2.0";
    

    Add a private instance variable for the client:

    ate OutlookClient mClient;
    

    And finally complete the onSuccess method by constructing a client and using it. We'll define the getMessages() method in the next step.

    rride
    ic void onSuccess(Boolean result) {
    mClient = new OutlookClient(outlookBaseUrl, mResolver);
    //call methods with the client.
    
    
  6. Create a new method to use the client to get all messages from your inbox.

    ected void getMessages() {
    Futures.addCallback(
            mClient.getMe()
            .getMessages()
            .top(20)
            .read(),
            new FutureCallback<List<Message>>() {
                @Override
                public void onSuccess(final List<Message> result) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            messagesTextView.setText("Messages: " + result.size());
                        }
                    });
                }
    
                @Override
                public void onFailure(final Throwable t) {
                    Log.e("getMessages", t.getMessage());
                }
            });
    
    

If successful, the number of retrieved messages from your inbox will be displayed in the TextView. :)

FAQ
Contributing

You will need to sign a Contributor License Agreement before submitting your pull request. To complete the Contributor License Agreement (CLA), you will need to submit a request via the form and then electronically sign the Contributor License Agreement when you receive the email containing the link to the document. This needs to only be done once for any Microsoft Open Technologies OSS project.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

License

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.