Microsoft 365 & Microsoft Graph library for Python
📌 Python Requirement: Python 3.8 or newer.
pip install office365-rest-python-clientOr with uv:
uv pip install office365-rest-python-clientThe latest version from GitHub can be installed directly:
pip install git+https://github.com/vgrem/office365-rest-python-client.git
The library provides two clients: ClientContext for SharePoint REST API and GraphClient for Microsoft Graph API.
📌 ACS Retirement Notice: Azure Access Control Service (ACS) for SharePoint is being retired. ACS stopped working for new tenants on November 1, 2024, and will be fully retired on April 2, 2026. Use Azure AD-based authentication instead. Learn more
ctx = ClientContext('{site_url}').with_client_certificate(tenant, client_id, thumbprint, cert_path)Uses the OAuth 2.0 Resource Owner Password Credentials grant via MSAL.
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext('{site_url}').with_username_and_password(
tenant='{tenant}', client_id='{client_id}',
username='{username}', password='{password}'
)Opens a browser for user login.
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext('{site_url}').with_interactive('{tenant}', '{client_id}')Prerequisite: configure Redirect URI as
http://localhostin Azure Portal.
User authenticates on another device via a displayed code.
ctx = ClientContext('{site_url}').with_device_flow('{tenant}', '{client_id}')Deprecated:
with_user_credentialsuses the legacy SAML-based auth flow which is being phased out by Microsoft. Usewith_username_and_password(MSAL ROPC OAuth 2.0) instead.
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext('{site_url}').with_user_credentials('{username}', '{password}')Replaced by:
ctx.with_username_and_password(tenant='{tenant}', client_id='{client_id}', username='{username}', password='{password}')Deprecated: Azure ACS is being retired. Use Azure AD certificate auth instead.
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext('{site_url}').with_credentials(ClientCredential('{client_id}', '{client_secret}'))Uses the OAuth 2.0 Client Credentials grant via MSAL.
from office365.graph_client import GraphClient
client = GraphClient(tenant='{tenant}').with_client_secret(client_id='{client_id}', client_secret='{client_secret}')client = GraphClient(tenant='{tenant}').with_client_certificate(client_id, thumbprint, private_key)Opens a browser for user login.
client = GraphClient(tenant='{tenant}').with_token_interactive(client_id='{client_id}')client = GraphClient(tenant='{tenant}').with_username_and_password('{client_id}', '{username}', '{password}')Any OAuth2-compliant library (MSAL, ADAL, etc.):
def acquire_token():
# your token acquisition logic
return token
client = GraphClient(acquire_token)from office365.sharepoint.client_context import ClientContext
site_url = "https://{tenant}.sharepoint.com"
ctx = ClientContext(site_url).with_username_and_password(
tenant="{tenant}", client_id="{client_id}",
username="{username}", password="{password}",
)
web = ctx.web.get().execute_query()
print(f"Web title: {web.title}")Add the environment parameter for non-global clouds:
from office365.azure_env import AzureEnvironment
ctx = ClientContext('{site_url}', environment=AzureEnvironment.USGovernmentHigh).with_credentials(...)For a comprehensive list, see the SharePoint examples guide.
from office365.graph_client import GraphClient
client = GraphClient(tenant='{tenant}').with_client_secret(client_id='{client_id}', client_secret='{client_secret}')
me = client.me.get().execute_query()
print(f"User: {me.user_principal_name}")from office365.graph_client import GraphClient
client = GraphClient(tenant='{tenant}').with_client_secret(client_id='{client_id}', client_secret='{client_secret}')
client.me.send_mail(
subject="Meet for lunch?",
body="The new cafeteria is open.",
to_recipients=["user@contoso.onmicrosoft.com"]
).execute_query()Send email | List messages | Download | Search
More Outlook examples.
from office365.graph_client import GraphClient
client = GraphClient(tenant='{tenant}')
drives = client.drives.get().execute_query()
for drive in drives:
print(f"Drive url: {drive.web_url}")Download files | Upload files | List drives
More OneDrive examples.
from office365.graph_client import GraphClient
client = GraphClient(tenant='{tenant}')
new_team = client.groups["{group_id}"].add_team().execute_query()Create a team | List teams | Send messages
More Teams examples.
from office365.graph_client import GraphClient
client = GraphClient(tenant='{tenant}')
with open("./MyPage.html", 'rb') as f:
page = client.me.onenote.pages.add(presentation_file=f).execute_query()from office365.graph_client import GraphClient
client = GraphClient(tenant='{tenant}')
task = client.planner.tasks.add(title="New task", planId="--plan-id--").execute_query()The following libraries will be installed when you install the client library: