Microsoft Graph Python SDK
raw JSON → 1.1.8.1 verified Sat May 09 auth: no python
API wrapper for Microsoft Graph written in Python. Version 1.1.8.1 supports Python 3.7+ and provides access to Microsoft 365 APIs. This library wraps Microsoft Graph REST API with easy-to-use methods for users, groups, mail, calendar, files, and more. Release cadence is low; the official Microsoft Graph SDK is recommended for new projects.
pip install microsoftgraph-python Common errors
error AttributeError: module 'microsoftgraph' has no attribute 'GraphClient' ↓
cause Importing old class name GraphClient after version 1.0.1 where it was renamed to GraphServiceClient.
fix
Use
from microsoftgraph.client import GraphServiceClient instead of from microsoftgraph import GraphClient. error msal.exceptions.MsalServiceError: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret' ↓
cause When using client credentials flow, you must provide either client_secret or certificate.
fix
Ensure you pass client_secret to GraphServiceClient or use a certificate. For device code or interactive flows, use PublicClientApplication.
Warnings
deprecated This library is not the official Microsoft Graph SDK. Microsoft's official SDK is `msgraph-sdk-python`. This library may lack updates and community support. ↓
fix Consider migrating to msgraph-sdk-python (pip install msgraph-sdk-python). See https://github.com/microsoftgraph/msgraph-sdk-python
gotcha GraphServiceClient does not handle token refresh automatically. You must manage token lifecycle with msal. ↓
fix Use msal's PublicClientApplication or ConfidentialClientApplication to acquire and refresh tokens, then pass the token to GraphServiceClient for each request.
breaking In version 1.0.1, the import path changed from microsoftgraph.client import GraphClient to microsoftgraph.client import GraphServiceClient. Old code using GraphClient will break. ↓
fix Change import to `from microsoftgraph.client import GraphServiceClient` and use GraphServiceClient class.
Imports
- GraphServiceClient wrong
from microsoftgraph import GraphClientcorrectfrom microsoftgraph.client import GraphServiceClient - DeviceCodeCredential wrong
from microsoftgraph.auth import DeviceCodeCredentialcorrectfrom msal import PublicClientApplication
Quickstart
from microsoftgraph.client import GraphServiceClient
from msal import PublicClientApplication
app = PublicClientApplication(client_id='YOUR_CLIENT_ID', authority='https://login.microsoftonline.com/common')
result = app.acquire_token_interactive(scopes=['User.Read'])
client = GraphServiceClient(tenant_id='YOUR_TENANT_ID', client_id='YOUR_CLIENT_ID', client_secret='YOUR_CLIENT_SECRET') if 'client_secret' else None
# If using device code flow
scopes = ['User.Read']
result = app.acquire_token_by_device_flow(scopes)
client = GraphServiceClient() if result else None