Upload Files to OneDrive from Linux with Python and Microsoft Graph
Question details
The user wants API guidance for uploading files to OneDrive from Linux using Python or another programming language.

- Product
- Microsoft OneDrive and Microsoft Graph
- Device & OS
- Linux
- Scenario
- A developer wants to programmatically upload local files to OneDrive from a Linux system.
- Observed behavior
- The goal is to authenticate with Microsoft Graph and upload files through the OneDrive API.
Have a Microsoft account with access to OneDrive and permission to register an application in Microsoft Entra. For the Python example, install Python and pip, and identify a local file to upload.
Upload a File with Python and Microsoft Graph
Use delegated OAuth authentication and a single upload request for a file up to 250 MB.
In the Microsoft Entra admin center, open Identity > Applications > App registrations and select New registration. Choose the account types appropriate for your OneDrive account, register the app, and copy its Application (client) ID.
In the app registration, open Authentication and enable Allow public client flows. Under API permissions, add the Microsoft Graph delegated permission Files.ReadWrite; obtain administrator consent if your organization requires it.
In a Linux terminal, run `python3 -m pip install msal requests`. Put a file named `example.txt` in the directory where you will run the script.
Save the following as `upload.py`, replace `YOUR_CLIENT_ID` with the application ID, and run `python3 upload.py`. Follow the displayed sign-in instructions in a browser. ```python import os from urllib.parse import quote import msal import requests client_id = 'YOUR_CLIENT_ID' file_path = 'example.txt' app = msal.PublicClientApplication( client_id, authority='https://login.microsoftonline.com/common' ) flow = app.initiate_device_flow(scopes=['Files.ReadWrite']) if 'user_code' not in flow: raise RuntimeError(str(flow)) print(flow['message']) result = app.acquire_token_by_device_flow(flow) if 'access_token' not in result: raise RuntimeError(str(result)) name = quote(os.path.basename(file_path), safe='') url = f'https://graph.microsoft.com/v1.0/me/drive/root:/{name}:/content' headers = { 'Authorization': 'Bearer ' + result['access_token'], 'Content-Type': 'application/octet-stream', } with open(file_path, 'rb') as file: response = requests.put(url, headers=headers, data=file, timeout=300) response.raise_for_status() print('Uploaded:', response.json()['name']) ```

Use an Upload Session for Large OneDrive Files
Use a resumable upload session when a file is too large for a single request or a chunked upload is preferable.
Work with Office Files Using WPS Office
WPS Office is a free, lightweight alternative with a familiar interface and support for Microsoft Office formats, making it easier to move documents between office suites. Microsoft Graph is still required for the OneDrive API upload workflow.

Frequently Asked Questions
Can I upload to OneDrive from Linux without Python?
Yes. Microsoft Graph is a REST API, so you can use another language or an HTTP client. Obtain an OAuth token, then send the appropriate upload request to the Graph endpoint.
Which permission does the Python example need?
Add the Microsoft Graph delegated permission Files.ReadWrite under App registrations > API permissions. An administrator may need to grant consent for an organization account.
How do I upload a file larger than 250 MB?
Create an upload session with the `createUploadSession` endpoint, then PUT sequential file chunks to the returned upload URL. Check the session status to resume an interrupted upload before it expires.
Will the sample overwrite an existing file?
The single-request example can replace a file with the same name in the OneDrive root. For an upload session, specify a conflict behavior such as rename when creating the session.
Can this method work with OneDrive for Business?
Yes. Microsoft Graph supports OneDrive for Business. Register the app for the appropriate organizational accounts and ensure the user or administrator has granted the required permissions.




