o365 devcamp los angeles june 16, 2015 module 06 hook into sharepoint apis with andfroid

31
Office DEVCamp – Los Angeles Module 6: Hooking into SharePoint APIs with Android Registration for Los Angeles June 16 2015 http://aka.ms/O365campLA

Upload: ivan-sanders

Post on 07-Aug-2015

93 views

Category:

Internet


0 download

TRANSCRIPT

Office DEVCamp – Los AngelesModule 6: Hooking into SharePoint APIs with AndroidRegistration for Los Angeles June 16 2015http://aka.ms/O365campLA

Course Agenda

Office Camp

Module 1: Introduction to the Day

Module 2: Setting up the Environments

Module 3: Hooking into Apps for SharePoint

Module 4: Hooking into Office 365 APIs

Module 5: Hooking into Apps for Office

Module 6: Hooking into SharePoint APIs with Android

Hooking into SharePoint APIs with Android

Ivan SandersSharePoint MVP/[email protected]@iasanders

Azure AD for AndroidO365 SharePoint for AndroidPatterns for consuming these APIs from Android

Agenda

Introduction

6

We’ll be developing using the Android Studio IDE, available for free from Google.

http://developer.android.com/sdk/index.html

Note: an alternative is the Eclipse IDE using the Android Developer Tools (ADT) plugin, though the ADT plugin is no longer under active development.

IDE – Android Studio

7

Android Studio is tightly integrated with the Gradle build system for configuring and building Android projects.

Each project has a number of .gradle files, which describe your project’s dependencies and which tool/SDK versions to use.

Including an external dependency is as simple as adding a line to a .gradle file. E.g.

Android Studio projects

8

The Android SDK is divided into API “Levels”. An app can be compatible with multiple API levels, through the use of support libraries.

The Android SDK includes the “SDK Manager”, for installing the SDK for each API Level.

You can launch the SDK Manager from Android Studio.

Android SDK

9Android Client

Azure AD O365 SharePoint

Linked

JSON/RESTOAuth

High level architecture

Authentication with Azure AD

11Android Client

Azure AD O365 SharePoint

Linked

JSON/RESTOAuth

Authentication with Azure AD

12

O365 SharePoint allows direct authentication via Azure Active Directory.

Azure AD supports the OAuth authentication protocol.

We will use the Active Directory Authentication Library for Android to authenticate.

Overview

13

Provides UI and services for implementing an Azure AD OAuth flow within an Android app.

https://github.com/AzureAD/azure-activedirectory-library-for-android

Active Directory Authentication Library

14

In order to authenticate with Azure AD we must create an Application within Active Directory.

The Application represents the Android client, and is granted permission to access O365 SharePoint.

We then configure the ADAL using information from this Application (client id, redirect address, requested resource).

Preparation

15

Using the library

dependencies {

//Include the Active Directory Authentication Library compile group: 'com.microsoft.aad', name: 'adal', version: '1.0.5'

}

Be sure to hit the Sync Now button:

Step 1: Add a dependency to build.gradle

16

Step 2: Update your project’s AndroidManifest.xml:

Using the library

<uses-permission android:name="android.permission.INTERNET" />

<application ...> ... <activity android:name="com.microsoft.aad.adal.AuthenticationActivity" android:label="Authenticate with AD" android:screenOrientation="portrait"> </activity></application>

17

Step 3: Create an instance of AuthenticationContext

Step 4: Handle authentication completion

Using the library

//in Activityprotected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //complete any authentication requests mContext.onActivityResult(requestCode, resultCode, data);}

//in Activity.onCreate()mContext = new AuthenticationContext(application, authority, false);

18

Step 5: Acquire a token (prompts the user to sign in)

Results in an Access Token, Refresh Token and User Id.

Using the library

mAuthContext.acquireToken(currentActivity, sharepointUrl, //e.g. http://mydomain.sharepoint.com clientId, //an Azure AD client id redirectUrl, //e.g. "http://android/callback" (also configured with AD) loginHint, //e.g. "[email protected]" promptBehaviour, extraQueryArgs, callback //e.g. new AuthenticationCallback<AuthenticationResult>() {...});

19

Step 6: Refresh our Access Token (if it expires).

This method does not prompt the user to sign in!

Using the library

mAuthContext.acquireTokenSilently( resource, clientId, userId, //acquired by call to acquireToken callback);

20

Authentication pattern – app start

Start

Show splash screen

acquireToken()AuthenticationActivity

Show main screen

End

storeTokens() callback(accessToken)

21

Authentication pattern – API calls* Start

User initiates API call

acquireTokenSilently()

callback(accessToken)

Complete API call

End

Cache valid? NO

YES

refreshToken()

Success?YES NO

Restart app for auth

End

storeTokens()

*This workflow can be wrapped into a few helper methods

AUTHENTICATION WITH AZURE AD

demo

O365 SharePoint for Android

24Android Client

Azure AD O365 SharePoint

Linked

JSON/RESTOAuth

Consuming the O365 SharePoint API

25

O365 SharePoint exposes REST APIs for lists and other content.

The Office 365 SDK for Android provides a library which allows for direct consumption of these APIs from an Android client.

Overview

26

Provides classes for asynchronously consuming the O365 SharePoint REST API.

https://github.com/OfficeDev/Office-365-SDK-for-Android

Office 365 SDK for Android

27

Step 1: Create a credentials object w/ the Access Token

Step 2: Create an instance of ListClient

Using the library

ListClient client = new ListClient( sharePointUrl, //e.g. "http://mydomain.sharepoint.com/" sharePointSitePath, //e.g. "/client/site" credentials);

Credentials credentials = new OAuthCredentials(accessToken);

28

To query for a list:

Using the library

final String listName = "My List";

//this produces the odata query: $filter=Title+eq+"My list"Query query = QueryOperations.field("Title").eq(listName);

//we can attach callbacks to f which will run when the data is availableListenableFuture<List<SPList>> f = client.getLists(query);

//Or... calling .get() will block this thread until the data is available//DO NOT DO THIS ON THE UI THREAD!List<SPList> results = f.get();

29

To query for list items:

Using the library

final String listName = "My List";

//this produces the odata query: $filter=startsWith(Title,"Item #")Query query = QueryOperations.startsWith("Title", "Item #");

//we can attach callbacks to f which will run when the data is availableListenableFuture<List<SPListItem>> f = client.getListItems(listName, query);

//Or... calling .get() will block this thread until the data is available//DO NOT DO THIS ON THE UI THREAD!List<SPListItem> results = f.get();

O365 SHAREPOINT FOR ANDROID

demo

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.