{"id":1257,"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":"python","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"},{"fix":"Ensure that all required environment variables, such as SHAREPOINT_SITE_URL, M365_CLIENT_ID, and M365_CLIENT_SECRET, are correctly defined and accessible in the execution environment before running your script.","message":"Essential environment variables (e.g., SHAREPOINT_SITE_URL, M365_CLIENT_ID, M365_CLIENT_SECRET) must be set for the library to initialize and authenticate successfully. Failure to set these will prevent any operations.","severity":"breaking","affected_versions":"All versions"},{"fix":"Ensure that all required environment variables, such as `SHAREPOINT_SITE_URL`, `M365_CLIENT_ID`, and `M365_CLIENT_SECRET`, are correctly set in your execution environment before running the application.","message":"The library requires specific environment variables (e.g., SHAREPOINT_SITE_URL, M365_CLIENT_ID, M365_CLIENT_SECRET) to be set for proper initialization and authentication. Failing to set these will prevent the library from operating.","severity":"breaking","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'2.6.2':65 '365':21,67,69 'activ':55 'ad':79 'api':25,36,77 'autom':80 'azur':78 'client':5,11 'comprehens':14 'current':62 'endpoint':41 'frequent':58 'graph':24,40,72 'interact':18 'interfac':30 'legaci':33 'librari':16,53 'like':44 'maintain':56 'microsoft':20,23,39,66,71 'modern':38 'offic':68 'office365':2,8 'office365-rest-python-client':1,7 'onedr':47,73 'onenot':49 'outlook':46,74 'planner':51 'provid':27 'python':4,10,15 'releas':59 'rest':3,9,35,76 'servic':43 'sharepoint':34,45,70 'support':42 'team':48,75 'unifi':29 'version':64","created_at":"2026-04-06T16:56:46.540289+00:00","updated_at":"2026-04-16T17:31:34.099812+00:00","problems":[{"fix":"Ensure both `office365-REST-Python-Client` and potentially `office365` are installed, and use correct import statements. For example, `pip install office365-REST-Python-Client office365`. Then, import classes from `office365.sharepoint.client_context` or `office365.graph_client` as needed.","cause":"This error often occurs because developers try to import a module named 'office365' directly, but the primary package is named `office365-REST-Python-Client`. While `office365` can be installed as a separate package, the core classes are typically imported from the `office365` namespace provided by `office365-REST-Python-Client` itself, and users may miss installing the correct or all necessary packages.","error":"ModuleNotFoundError: No module named 'office365'"},{"fix":"Verify that the `client_id` and `client_secret` are correct and unexpired, and that the Azure AD App Registration or SharePoint App-Only principal has the necessary API permissions (e.g., `Sites.FullControl.All`, `Sites.Read.All`). For SharePoint App-Only, ensure permissions are granted via `_layouts/15/appinv.aspx`. Ensure that any tenant-level restrictions or MFA policies are not preventing programmatic access.","cause":"This is a common authentication or authorization failure, often stemming from incorrect client ID or client secret, expired credentials, insufficient API permissions for the registered application in Azure AD or SharePoint, or issues with multi-factor authentication (MFA) blocking automated access.","error":"401 Client Error: Unauthorized"},{"fix":"Update your code to use the current API methods as per the library's documentation or examples for your installed version. For the `_auth_context` issue, it usually points to an internal library bug that has been fixed in newer versions. For `connect_with_certificate`, use `ClientContext.with_client_certificate`. Upgrading the library to the latest version (`pip install --upgrade office365-REST-Python-Client`) often resolves such `AttributeError`s related to internal structure changes.","cause":"This error typically indicates that the code is attempting to access an attribute or method that either does not exist in the current version of the `ClientContext` object or has been renamed due to API changes or refactoring in the library. For example, `connect_with_certificate` was renamed to `with_client_certificate`.","error":"AttributeError: 'ClientContext' object has no attribute '_auth_context' (or similar, e.g., 'connect_with_certificate')"},{"fix":"Adjust your import statements to reflect the current module structure. For `ClientCredential`, the correct import is often `from office365.runtime.auth.client_credential import ClientCredential`. Similarly, `UserCredential` is `from office365.runtime.auth.user_credential import UserCredential`. Also, ensure you have the latest version of the library installed.","cause":"This error occurs when the import path for certain authentication classes, like `ClientCredential` or `UserCredential`, has changed in newer versions of the library due to internal restructuring. Older code examples or outdated installations might reference the wrong path.","error":"ModuleNotFoundError: No module named 'office365.runtime.auth.ClientCredential' (or 'AuthenticationContext', 'UserCredential')"},{"fix":"Double-check the exact server-relative URL or guest URL of the file/folder. Ensure the `ClientContext` is initialized with the correct site URL that hosts the content. For large lists, consider if SharePoint's list view threshold is being hit and if indexing columns could help. Verify that the application's granted permissions extend to the specific document library or folder being accessed, not just the site collection.","cause":"Even after successful authentication, developers may encounter 'File Not Found' or other access errors when trying to interact with specific files or folders. This can be due to an incorrect file path (relative vs. server-relative URL), a mismatch between the site URL used for `ClientContext` and the actual location of the resource, SharePoint list view threshold issues for large libraries, or insufficient permissions for the *specific* SharePoint site/document library despite having general access.","error":"File Not Found (or similar errors when accessing files/folders)"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"3.0.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/vgrem/Office365-REST-Python-Client","docs":null,"changelog":null,"pypi":"https://pypi.org/project/office365-rest-python-client/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["crm-productivity","communication","http-networking","azure"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}