Integrating Microsoft Graph API Examples

Explore practical examples of integrating Microsoft Graph API for Office 365 services.
By Jamie

Integrating Microsoft Graph API for Office 365 Services

Integrating with Microsoft Graph API allows developers to seamlessly access and manage Office 365 services, such as Outlook, OneDrive, and Microsoft Teams. This powerful API provides a unified endpoint for interacting with various Microsoft 365 services, enabling enhanced productivity and integrated workflows. Below are three practical examples that illustrate how to integrate Microsoft Graph API in different contexts.

Example 1: Accessing User Emails from Outlook

Use Case

In a corporate environment, developers may need to create applications that access user emails to automate the retrieval of important messages or to analyze communication patterns.

To get started, you’ll need to register your application in the Azure portal to obtain the necessary credentials (Client ID and Secret). After that, use the following code snippet to access user emails:

import requests

## Replace with your values
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
tenant_id = 'YOUR_TENANT_ID'

## Get access token
url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'
data = {
    'client_id': client_id,
    'client_secret': client_secret,
    'scope': 'https://graph.microsoft.com/.default',
    'grant_type': 'client_credentials'
}
response = requests.post(url, data=data)
token = response.json().get('access_token')

## Get user emails
user_id = 'USER_EMAIL'
headers = {'Authorization': f'Bearer {token}'}
email_url = f'https://graph.microsoft.com/v1.0/users/{user_id}/messages'
email_response = requests.get(email_url, headers=headers)
print(email_response.json())

Notes

  • Ensure the application has the required permissions (Mail.Read) in the Azure portal.
  • The response will contain a JSON object with user emails, which can be further processed or displayed.

Example 2: Uploading Files to OneDrive

Use Case

In scenarios where users need to upload files to OneDrive programmatically, integrating Microsoft Graph API simplifies this process, enabling seamless file management across devices.

First, acquire the access token as shown in Example 1. Then, use the code below to upload a file to OneDrive:

import requests

## After obtaining the token from Example 1
access_token = 'YOUR_ACCESS_TOKEN'

## Upload file to OneDrive
file_name = 'example.txt'
file_content = 'Hello, this is a test file.'

upload_url = f'https://graph.microsoft.com/v1.0/me/drive/root:/{file_name}:/content'
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'text/plain'}
response = requests.put(upload_url, headers=headers, data=file_content)
print(response.json())

Notes

  • The endpoint used here uploads the file to the root of the user’s OneDrive.
  • Adjust the path in the URL if you want to upload to a specific folder.

Example 3: Creating a Microsoft Teams Channel

Use Case

For organizations using Microsoft Teams, automating the creation of Teams channels can be beneficial for project management or team collaboration.

After obtaining the access token, you can create a new channel in a specific Team using the following code:

import requests

## After obtaining the token from Example 1
access_token = 'YOUR_ACCESS_TOKEN'

## Create a new channel in Teams
team_id = 'YOUR_TEAM_ID'
channel_name = 'New Project Channel'
channel_description = 'This channel is for discussing the new project.'

create_channel_url = f'https://graph.microsoft.com/v1.0/teams/{team_id}/channels'
data = {
    'displayName': channel_name,
    'description': channel_description
}
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}
response = requests.post(create_channel_url, headers=headers, json=data)
print(response.json())

Notes

  • Ensure that the application has the necessary permissions (Channel.Create) to create channels in Teams.
  • The response will include details of the created channel, which can be used for further actions.

Conclusion

These examples of integrating Microsoft Graph API for Office 365 services demonstrate the versatility and power of this API. By leveraging Graph API, developers can create applications that enhance productivity and streamline workflows across the Microsoft ecosystem.