Office365-REST-Python-Client
The Office365-REST-Python-Client is a comprehensive Python library for interacting with Microsoft 365 and Microsoft Graph APIs. It provides a unified interface for both legacy SharePoint REST APIs and modern Microsoft Graph endpoints, supporting services like SharePoint, Outlook, OneDrive, Teams, OneNote, and Planner. The library is actively maintained, with frequent releases, and is currently at version 2.6.2.
Warnings
- breaking Username and password authentication (with_user_credentials) may fail or require frequent re-authentication in MFA-enabled environments. In version 2.6.0, support for automatically renewing authentication cookies for `with_user_credentials` was added, which mitigates some issues but does not resolve all MFA challenges.
- gotcha API calls are often queued and not executed until `execute_query()` is explicitly called. Forgetting this can lead to unexpected behavior or no data being retrieved.
- gotcha Incorrect or insufficient Azure AD application permissions (e.g., 'Sites.Read.All', 'Mail.Send', 'Files.ReadWrite.All') or missing admin consent will result in 401 Unauthorized or 403 Forbidden errors when accessing resources.
- gotcha When working with SharePoint, ensure the `sharepoint_site_url` points to the correct site collection or subsite. Using a generic tenant URL for operations on a specific subsite can lead to 'File Not Found' or permission-related errors.
- gotcha Retrieving items from large collections (e.g., lists, document libraries) may require explicit pagination handling. While the library offers `paged()` methods, older approaches or custom queries might miss items if the collection exceeds the default page size.
- gotcha Updating SharePoint lookup columns requires passing a `FieldLookupValue` object, not just the lookup item's ID directly. Direct assignment of an ID will fail.
Install
-
pip install Office365-REST-Python-Client
Imports
- ClientContext
from office365.sharepoint.client_context import ClientContext
- GraphClient
from office365.graph_client import GraphClient
- ClientCredential
from office365.runtime.auth.client_credential import ClientCredential
- UserCredential
from office365.runtime.auth.user_credential import UserCredential
- AzureEnvironment
from office365.azure_env import AzureEnvironment
Quickstart
import os
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential
# --- Configuration (replace with your actual values or set as environment variables) ---
sharepoint_site_url = os.environ.get('SHAREPOINT_SITE_URL', 'https://yourtenant.sharepoint.com/sites/yoursite')
client_id = os.environ.get('M365_CLIENT_ID', '')
client_secret = os.environ.get('M365_CLIENT_SECRET', '')
if not all([sharepoint_site_url, client_id, client_secret]):
print("Error: Please set SHAREPOINT_SITE_URL, M365_CLIENT_ID, and M365_CLIENT_SECRET environment variables.")
exit(1)
try:
# Initialize ClientContext with app-only credentials
ctx = ClientContext(sharepoint_site_url).with_credentials(ClientCredential(client_id, client_secret))
# Load the web object and execute the query
ctx.load(ctx.web)
ctx.execute_query()
print(f"Successfully connected to SharePoint site: {ctx.web.url}")
print(f"Web title: {ctx.web.title}")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure your Azure AD application is registered, has the necessary API permissions (e.g., 'Sites.Read.All' or 'Sites.FullControl.All'), and admin consent has been granted.")