Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

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 REST calls directly to the Microsoft Graph endpoint, and the Active Directory Authentication Library (ADAL) for authentication of users' work or school Office 365 accounts.

License

Notifications You must be signed in to change notification settings

microsoftgraph/android-java-snippets-rest-sample

Repository files navigation

Office 365 Android Microsoft Graph Snippets

Table of contents

Looking to build cool apps that help people work with their Office 365 data? Explore, learn, and understand the Microsoft Graph (previously called Office 365 unified API) REST APIs by using the Android Microsoft Graph API REST Snippets sample. This sample shows you how to access multiple resources, including Microsoft Azure Active Directory and the Office 365 APIs, by making HTTP requests to the Microsoft Graph API in an Android application.

You can explore the following operations for Office 365:

Me

Users

Events

Messages

Contacts

Groups

Drives

Device requirement

To run the Microsoft Graph API REST snippets project, your device must meet the following requirement:

  • Android API level 21 or newer

Prerequisites

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

Azure client application registration

  1. Open a browser and navigate to the Azure Active Directory admin center. Login using a Work or School Account.

  2. Select Azure Active Directory in the left-hand navigation, then select App registrations under Manage.

    A screenshot of the App registrations

  3. Select New registration. On the Register an application page, set the values as follows.

    • Set a preferred Name e.g. AndroidSnippets
    • Set Supported account types to Accounts in any organizational directory.

    A screenshot of the Register an application page

  4. Choose Register. On the AndroidSnippets app page, select Overview and copy the value of the Application (client) ID and save it, you will need it in the next step.

    A screenshot of Application Id

  5. Still on the app page, select Authentication. Locate the section Redirect URIs. In the Suggested Redirect URIs for public clients(mobile,desktop), check the second box so that the app can work with the MSAL libraries used in the application. (The box should contain the option msal<YOUR_CLIENT_ID>://auth). Choose Save.

    A screenshot of Suggested Redirect URIs for Public Client

To learn about authenticating with MSAL for Android to make calls to Microsoft Graph, see Call the Microsoft Graph API from an Android app.

Configure the project

  1. Download or clone the Android Microsoft Graph 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 O365-Android-Unified-API-Snippets 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. Open the ServiceConstants.java file in the folder java > com.microsoft.office365.msgraphsnippetapp.
  7. Find the CLIENT_ID string and replace the value ENTER_YOUR_CLIENT_ID with the Application (client) ID that you copied in the previous step.
  8. Open the AndroidManifest.xml file in the app > manifests folder.
  9. Replace the ENTER_YOUR_CLIENT_ID with the Application (client) ID that you copied in the previous step.

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 Office 365 button to sign in to Office 365.
  3. Enter your credentials.
  4. Review and accept the permission scopes requested by the app.
  5. Click a REST operation in the main activity to show operation details.
  6. Click the RUN button to start the REST operation and wait for the operation to finish.
  7. Click in the Response Headers or Response Body text boxes to copy the box contents to the emulator/device clipboard.
  8. Click the back button on the REST Explorer toolbar to return to the REST operation list.
  9. (Optional) Click the overflow menu to get the Disconnect menu option.

How the sample affects your tenant data

This sample runs REST 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 Office 365 Android unified API snippets project uses these classes to manage interactions with the Microsoft Graph API on Office 365:

Sample project organization

The Office 365 Android unified API snippets project is comprised of three 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 Office 365 Android unified API snippets app module as an example of how to call methods in the other sample modules.

Modules in the Office 365 Android unified API snippets project

  • o365-Auth. This module contains the library calls to authenticate a user with Office 365.
  • unifiedapi. This module encapsulates the Retrofit REST operations used for the Office 365 Microsoft Graph API endpoint.
  • app. The UI and business logic module. REST operations are started in the snippet classes in this module.

Snippet classes

A snippet runs a single REST 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 API service classes described below.

Microsoft Graph API service classes

These classes are found in the unifiedapi module and make the Retrofit library calls that generate the REST queries and handle operation results. These service classes are consumed by the snippets.

Authentication classes for Office 365 business accounts

The authentication classes are found in the o365-Auth module. These classes use the Microsoft Azure Active Directory Library (ADAL) for Android to connect to a business version of Office 365 such as Office 365 Enterprise.

Questions and comments

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

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.

Additional resources

Copyright

Copyright (c) 2019 Microsoft. All rights reserved.

About

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 REST calls directly to the Microsoft Graph endpoint, and the Active Directory Authentication Library (ADAL) for authentication of users' work or school Office 365 accounts.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages