Permission & Access Issues

How to Download SharePoint Files with Python Authentication

Aamir Naveed AkramAamir Naveed Akram Nov 3, 2026 870 views

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.

How to Download SharePoint Files with Python Authentication
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.
Before you start

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.

Solution 1Recommended

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.

1
Install the SharePy library

Open your terminal or command prompt and run the command 'pip install sharepy' to install the required package.

2
Authenticate your session

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.

3
Request the file endpoint

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")'.

4
Save the file locally

Open a local file in write-binary mode ('wb') and write the response content ('r.content') to it to save the downloaded document.

Use the SharePy Library for SharePoint REST API
Session Persistence: SharePy saves your authentication cookie locally, so subsequent executions of your script will not require you to re-enter your password until the token expires.
Free Microsoft Office alternative

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. 1. Install WPS Office: Download the free, lightweight installer from the official WPS website and complete the setup in just a few minutes.
  2. 2. Locate your downloaded files: Find the documents your Python script retrieved and saved to your local machine.
  3. 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.
Fully compatible with Microsoft Word, Excel, and PowerPoint file formats (.docx, .xlsx, .pptx) downloaded from SharePoint.Lightweight application that opens large datasets or complex documents instantly without system lag.Familiar user interface ensures a zero-learning-curve transition from other Office software.All-in-one tabbed workspace to handle PDFs, spreadsheets, and presentations efficiently.
microsoft office alternative - wps office

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.