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.
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())
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())
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())
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.