android-java-snippets-sample icon indicating copy to clipboard operation
android-java-snippets-sample copied to clipboard

Code snippets that use the Microsoft Graph to perform common tasks, such as sending email or managing groups, from within an Android app. This sample uses the Microsoft Graph Client Library to work wi...

Snippets Sample for Android Using the Microsoft Graph SDK

Version 1.5

Build Status

Table of contents

  • Device requirement
  • Prerequisites
  • Azure client application registration
  • Configure the project
  • Run the project
  • How the sample affects your tenant data
  • Understand the code
  • Questions and comments
  • Additional resources
  • Version history

Looking to build cool apps that help people work with their Office 365 data? Explore, learn, and understand the Microsoft Graph SDK by using this Snippets sample. This sample shows you how to access multiple resources, including Microsoft Azure Active Directory and Office 365, by making calls to the Microsoft Graph SDK in an Android application.

You can explore the following operations in Microsoft Graph:

Me

  • Get information about the signed in user.
  • Get the user's responsibilities.
  • Get the user's manager.
  • Get the user's direct reports.
  • Get the user's group membership
  • Get the user's photo.

Users

  • Get users from your tenant's directory.
  • Get users filtered by criteria from your tenant's directory.
  • Create a new user.

Events

  • Get the signed-in user's events.
  • Create a new event for the user.
  • Update a user's event.
  • Delete a user's event.

Messages

  • Get user's messages.
  • Send a message from the user mailbox.

Groups

  • Get all groups in your tenant's directory.
  • Create a new group.
  • Get information about a specific group in the tenant.
  • Delete a group.
  • Get a group's members.
  • Get a group's owners.

Drives

  • Gets the signed-in user's drive.
  • Gets all of the files in the user's root folder.
  • Creates a new file in the user's root folder.
  • Downloads a file from the user's root folder.
  • Updates the contents of a file in the user's root folder.
  • Deletes a file in the user's root folder.
  • Renames a file in the user's root folder.
  • Creates a folder under the user's root folder.

Device requirements

To run snippets project, your device must meet the following requirement:

  • Android API level 21 or newer

Android Studio requirements

  • Android build tools for Gradle 3.1+

Note: Android Studio may prompt you to install the Android Support Repository 47.0.0+. The project's module build.gradle file logic uses the implementation method instead of the older compile method to build the module dependencies. These new build methods depend on the Android Support Repository 47.0.0 or newer.

Prerequisites

To use the Microsoft Graph SDK snippets project, you need the following:

  • The latest version of Android Studio.
  • The Gradle build automation system.
  • An Office 365 account. You can sign up for an Office 365 Developer subscription that includes the resources you need to start building Office 365 apps.
  • Java Development Kit (JDK) 7.
  • A registered Azure application with a client id and redirect URI value. See Grant permissions to the Snippets application in Azure for details about how to create the correct permissions.

Azure client application registration

  1. Navigate to the Azure portal - App registrations page.
  2. Select New registration.
  3. When the Register an application page appears, enter your application's registration information:
    • In the Name section, enter a meaningful application name that will be displayed to users of the app, for example Android Java Snippets Sample.
    • In the Supported account types section, select Accounts in any organizational directory.
  4. Select Register to create the application.
  5. On the app Overview page, find the Application (client) ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
  6. In the list of pages for the app, select Manifest, and:
    • In the manifest editor, set the allowPublicClient property to true
    • Select Save in the bar above the manifest editor.
  7. In the list of pages for the app, select API permissions
    • Click the Add a permission button and then,
    • Ensure that the Microsoft APIs tab is selected
    • In the Commonly used Microsoft APIs section, click on Microsoft Graph
    • In the Delegated permissions section, ensure that the right permissions are checked. Use the search box if necessary.
    • User.Read, Mail.Send, Mail.ReadWrite, Files.ReadWrite, User.ReadBasic.All, Calendars.ReadWrite, Group.Read.All
    • Select the Add permissions button

Note: In Configure the project, you'll be asked to copy some strings that are generated by the application registration portal. Be sure to stay on application registration page while you configure the project. The strings that you copy from the application registration will be pasted into the source code of the project.

Configure the project

  1. Download or clone the snippets sample.
  2. Start Android Studio.
  3. From the Welcome to Android Studio dialog box, choose Import project (Eclipse ADT, Gradle, etc).
  4. Select the settings.gradle file in the android-java-snippets-sample folder, and then click OK.
  5. Respond to the dialog box ("Gradle Sync: Gradle settings for this project are not configured yet. Would you like the project to use the Gradle wrapper? ") by clicking the OK button to use the Gradle wrapper.
  6. In the application registration page that you just visited, copy the custom redirect URI (msal{application id as GUID}) to the clipboard. Be sure you don't copy ://auth
    For example: msal0575d7fe-8ec7-4925-9ce2-87074778a039 is copied and ://auth is ignored.
  7. Paste the clipboard contents in app/src/main/AndroidManifest.xml, line 41 to replace ENTER_YOUR_CLIENT_ID with the clipboard contents.
  8. Copy the GUID portion of the custom redirect URI into app/src/main/AndroidManifest.xml, line 49 to replace ENTER_YOUR_CLIENT_ID with the clipboard contents.

Run the project

After you've built the project you can run it on an emulator or device.

  1. Run the project.
  2. Click the Connect to Microsoft Graph button to sign in.
  3. Enter your credentials.
  4. Review and accept the permission scopes requested by the app.
  5. Click an operation in the main activity to show operation details.
  6. Click the RUN button to start the operation and wait for the operation to finish.
  7. Click in the Raw Object text box to copy the box contents to the emulator/device clipboard.
  8. Click the back button on the toolbar to return to the operation list.
  9. (Optional) Click the overflow menu to get the Disconnect menu option.

How the sample affects your tenant data

This sample runs commands that create, read, update, or delete data. When running commands that delete or edit data, the sample creates fake entities. The fake entities are deleted or edited so that your actual tenant data is unaffected. The sample will leave behind fake entities on your tenant.

Understand the code

The snippets project uses these classes to manage interactions with Microsoft Graph:

Sample project organization

The snippets project is comprised of two modules. The modular design enables you to build a new app based on this sample by importing the modules into your app. After you've imported the modules, use the code in the snippets app module as an example of how to call methods in the other sample modules.

Modules in the snippets project

  • app. The UI and business logic module. SDK operations are started in the snippet classes in this module.

Snippet classes

A snippet runs a single operation and returns the results. Snippets are found in the app module. Snippets set the state required to make the calls on the Microsoft Graph service classes described below.

  • AbstractSnippet
  • DrivesSnippets
  • EventsSnippets
  • GroupsSnippets
  • MeSnippets
  • MessageSnippets
  • UsersSnippets

Authentication classes

The authentication methods are found in the AuthenticationManager class. These methods use the Microsoft Authentication Library (MSAL) for Android to connect to Microsoft Graph.

  • AuthenticationManager. Encapsulates user connect and disconnect logic in addition to app authorization.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Questions and comments

We'd love to get your feedback about the Microsoft Graph SDK Snippets sample for Android. You can send your feedback to us in the Issues section of this repository.
General questions about Microsoft Graph development should be posted to Stack Overflow. Make sure that your questions are tagged with [microsoftgraph].

Additional resources

Version history

Version Changes
1.0 Initial release
1.5 - MSAL authentication library replaced ADAL authentication library
- Microsoft Graph SDK version 1.5

Copyright (c) 2019 Microsoft. All rights reserved.