{"library":"office365-rest-python-client","title":"Office365-REST-Python-Client","description":"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.","status":"active","version":"2.6.2","language":"en","source_language":"en","source_url":"https://github.com/vgrem/Office365-REST-Python-Client","tags":["Microsoft 365","Office 365","SharePoint","Microsoft Graph","OneDrive","Outlook","Teams","REST API","Azure AD","Automation"],"install":[{"cmd":"pip install Office365-REST-Python-Client","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Used for HTTP requests.","package":"requests","optional":false},{"reason":"Default library for obtaining tokens for Microsoft Graph API authentication.","package":"msal","optional":false}],"imports":[{"note":"Used for interacting with SharePoint (legacy REST API).","symbol":"ClientContext","correct":"from office365.sharepoint.client_context import ClientContext"},{"note":"Used for interacting with Microsoft Graph API.","symbol":"GraphClient","correct":"from office365.graph_client import GraphClient"},{"note":"Used for app-only authentication with client ID and client secret.","symbol":"ClientCredential","correct":"from office365.runtime.auth.client_credential import ClientCredential"},{"note":"Used for username/password authentication (note: less secure and often problematic with MFA).","symbol":"UserCredential","correct":"from office365.runtime.auth.user_credential import UserCredential"},{"note":"Used to specify Azure cloud environments (e.g., US Government).","symbol":"AzureEnvironment","correct":"from office365.azure_env import AzureEnvironment"}],"quickstart":{"code":"import os\nfrom office365.sharepoint.client_context import ClientContext\nfrom office365.runtime.auth.client_credential import ClientCredential\n\n# --- Configuration (replace with your actual values or set as environment variables) ---\nsharepoint_site_url = os.environ.get('SHAREPOINT_SITE_URL', 'https://yourtenant.sharepoint.com/sites/yoursite')\nclient_id = os.environ.get('M365_CLIENT_ID', '')\nclient_secret = os.environ.get('M365_CLIENT_SECRET', '')\n\nif not all([sharepoint_site_url, client_id, client_secret]):\n    print(\"Error: Please set SHAREPOINT_SITE_URL, M365_CLIENT_ID, and M365_CLIENT_SECRET environment variables.\")\n    exit(1)\n\ntry:\n    # Initialize ClientContext with app-only credentials\n    ctx = ClientContext(sharepoint_site_url).with_credentials(ClientCredential(client_id, client_secret))\n    \n    # Load the web object and execute the query\n    ctx.load(ctx.web)\n    ctx.execute_query()\n    \n    print(f\"Successfully connected to SharePoint site: {ctx.web.url}\")\n    print(f\"Web title: {ctx.web.title}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    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.\")\n","lang":"python","description":"This quickstart demonstrates how to connect to a SharePoint site using application (client ID and secret) authentication via the `ClientContext`. It retrieves and prints the site's URL and title. Ensure your Azure AD application is registered with appropriate permissions (e.g., 'Sites.Read.All' or 'Sites.FullControl.All') and that admin consent is granted in the Azure Portal. It's recommended to use environment variables for sensitive credentials."},"warnings":[{"fix":"For unattended scripts, prefer Azure AD application-only authentication using client secrets or certificates (ClientContext.with_client_credentials or GraphClient.with_client_secret/with_certificate). For interactive scenarios, consider interactive authentication flows if supported by your organization's policies.","message":"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.","severity":"breaking","affected_versions":"All versions, especially with MFA."},{"fix":"Always call `.execute_query()` after building your API requests (e.g., after loading properties or calling methods that interact with the server).","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Verify that your Azure AD app registration has all necessary API permissions for Microsoft Graph or SharePoint, and ensure that 'Grant admin consent for [your tenant]' has been clicked in the Azure Portal for application permissions.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Provide the full and exact URL to the SharePoint site or subsite you intend to interact with in the `ClientContext` constructor.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For large collections, use the `.paged()` method available on item collections or ensure your iteration logic correctly handles continuation tokens for full retrieval.","message":"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.","severity":"gotcha","affected_versions":"<= 2.5.8 (improved in 2.5.9 for DriveItem.get_files/folders)"},{"fix":"Use `from office365.sharepoint.field_value import FieldLookupValue` and set the property like `item_to_update.set_property('Department', FieldLookupValue(lookup_id=1)).update().execute_query()`.","message":"Updating SharePoint lookup columns requires passing a `FieldLookupValue` object, not just the lookup item's ID directly. Direct assignment of an ID will fail.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-06T00:00:00.000Z","next_check":"2026-07-05T00:00:00.000Z"}