The `o365` Python library provides a simple and Pythonic interface for interacting with Microsoft Graph and Office 365 APIs. It supports access to various services including Email, Calendar, Contacts, OneDrive, and SharePoint. The library handles OAuth authentication, token refreshing, and datetime conversions automatically. It is actively maintained, with the current stable version being 2.1.9, and receives regular updates.
pip install O365Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to authenticate with Microsoft Graph using `o365` and then send a simple email. It uses environment variables for `CLIENT_ID`, `CLIENT_SECRET`, and `TENANT_ID` (if applicable) and shows how to set up `FileSystemTokenBackend` for persistent token storage. The default authentication flow is interactive, requiring user consent via a console-provided URL. Ensure your Azure AD application is registered with the correct redirect URI and API permissions (scopes).
Re-authenticate all existing `o365` applications to obtain new tokens. Ensure `msal` is correctly installed as a dependency.
Upgrade your Python environment to 3.10 or newer. If using the old query style, migrate to `QueryBuilder` for future compatibility, although the old query remains interchangeable for other methods for now.
Ensure your application is registered in Azure AD with the 'Web' platform and a redirect URI (e.g., `http://localhost:8000`). Grant the necessary delegated or application permissions (scopes, e.g., `Mail.ReadWrite`, `Mail.Send`, `offline_access`). For `auth_flow_type='credentials'`, you must provide the `tenant_id` and ensure appropriate application permissions are granted.
Implement a `TokenBackend` appropriate for your environment (e.g., `FileSystemTokenBackend` for local storage or cloud-specific backends) and ensure that the token storage location is protected. Consider using a `cryptography_manager` with your `TokenBackend` for encryption.
Ensure the `O365` package is installed in your current Python environment using `pip install O365`. Verify that your import statements use the correct casing, for example: `from O365 import Account`.
Verify that your `client_id` and `tenant_id` precisely match the Azure AD application registration, ensure the application is registered in the correct Microsoft Entra tenant, and confirm that all necessary API permissions have been granted and consented by an administrator.
Ensure you are using OAuth 2.0 for authentication. Verify your Azure AD app registration settings, including client ID, client secret, redirect URIs, and requested scopes. Make sure the authentication flow (e.g., interactive consent for 'on behalf of a user' flow or 'credentials' flow for daemon apps) is correctly implemented in your code.
Add checks in your code to verify that the object returned by an `o365` operation is not `None` before attempting to access its attributes. Debug the specific API call to ensure the resource exists and proper permissions are in place, for example: `if message and message.body:`.