How to Download SharePoint Files with Python Authentication
Question details
The user needs to download files from SharePoint using a Python script, which requires programmatic authentication rather than relying on an active browser session.

- Product
- SharePoint API
- Device & OS
- not provided
- Scenario
- Automating the retrieval of documents or datasets hosted on a corporate SharePoint site using a custom Python script.
- Observed behavior
- Links that successfully initiate a download in a web browser fail with unauthorized or access denied errors when accessed directly via Python HTTP requests.
Verify that you have the necessary organizational permissions to access the SharePoint site via API, and ensure your Python environment has a package manager (like pip) ready to install authentication libraries.
Use the SharePy Library for SharePoint REST API
SharePy is a lightweight Python library specifically designed to handle Microsoft SharePoint Online authentication, making it easy to establish an authenticated session.
Because standard Python request libraries do not inherit your browser's login state, you must explicitly authenticate. SharePy handles the complex authentication flows for SharePoint Online behind the scenes.
Open your terminal or command prompt and run the command 'pip install sharepy' to install the required package.
In your Python script, import sharepy and create an authenticated session by running 's = sharepy.connect("https://yourdomain.sharepoint.com")'. You will be prompted to enter your credentials.
Use the authenticated session to send a GET request to the file's API URL, for example: 'r = s.get("https://yourdomain.sharepoint.com/sites/YourSite/_api/web/GetFileByServerRelativeUrl('/sites/YourSite/Shared Documents/file.xlsx')/$value")'.
Open a local file in write-binary mode ('wb') and write the response content ('r.content') to it to save the downloaded document.

Use Microsoft Graph API with MSAL
For modern enterprise environments with strict security policies, using the Microsoft Graph API with the Microsoft Authentication Library (MSAL) is the most secure and scalable approach.
Seamlessly View and Edit Downloaded Files with WPS Office
After successfully downloading your SharePoint documents via Python, you need a reliable suite to manage them. WPS Office provides a free, lightweight alternative to Microsoft Office that offers seamless format compatibility and a familiar user interface for all your document needs.
- 1. Install WPS Office: Download the free, lightweight installer from the official WPS website and complete the setup in just a few minutes.
- 2. Locate your downloaded files: Find the documents your Python script retrieved and saved to your local machine.
- 3. Open and edit seamlessly: Launch WPS Office and open your files. Your document formatting, charts, and data will be perfectly preserved thanks to its high compatibility.

Frequently Asked Questions
Why do I get a 403 Forbidden error when using Python 'requests' for a SharePoint link?
A 403 error usually occurs because your programmatic request lacks the required authentication tokens. Browser cookies aren't automatically shared with a Python script, so you must explicitly authenticate using API tokens or libraries like SharePy.
Can I use standard username and password (Basic Auth) with Python to access SharePoint?
Microsoft has deprecated Basic Authentication for SharePoint Online to enhance security. You must use Modern Authentication (OAuth 2.0) through Microsoft Graph API, MSAL, or specialized tools designed for modern auth flows.
Do I need admin privileges to use the Microsoft Graph API for downloading files?
You need the appropriate API permissions assigned to your registered application in Azure AD. Depending on the scope of the data you are accessing, certain permissions (like Sites.Read.All) will require global admin consent.




