如何用Python OneDrive SDK上传文件?认证失败及重定向URI疑问
Hey there! Let's work through your two questions about using the Python OneDrive SDK (built on Microsoft Graph) clearly and practically.
First, you'll need to use the Microsoft Graph API (since the official OneDrive SDK leverages this) along with Azure Identity for authentication. Here's a complete workflow with code:
Step 1: Install required packages
Run this command to get the necessary libraries:
pip install msgraph-core azure-identity
Step 2: Authenticate and upload a file
Since you don't have a web app, we'll use the device code flow (perfect for desktop/command-line tools). Here's the code to authenticate and upload a local file to your OneDrive:
from azure.identity import DeviceCodeCredential from msgraph import GraphServiceClient from msgraph.generated.drives.item.items.item.upload_session.create_upload_session_post_request_body import CreateUploadSessionPostRequestBody import os # Replace with your app's client ID (get this from Azure AD App Registration) CLIENT_ID = "your-client-id-here" # Required scopes for OneDrive file upload access SCOPES = ["Files.ReadWrite.All"] # Initialize credential using device code flow (no web server needed) credential = DeviceCodeCredential(CLIENT_ID, scopes=SCOPES) graph_client = GraphServiceClient(credential, scopes=SCOPES) def upload_file_to_onedrive(local_file_path, onedrive_target_path): # Get size of the local file to handle chunked uploads file_size = os.path.getsize(local_file_path) # Configure upload session (replace existing file if it exists) request_body = CreateUploadSessionPostRequestBody( item={ "@microsoft.graph.conflictBehavior": "replace", "name": os.path.basename(onedrive_target_path) } ) # Create an upload session for the target OneDrive path upload_session = graph_client.drives.by_drive_id("me").items.by_drive_item_id(f"root:/{onedrive_target_path}:").create_upload_session.post(request_body) # Upload the file in chunks (handles large files automatically) with open(local_file_path, "rb") as file: upload_task = graph_client.drives.by_drive_id("me").items.by_drive_item_id(f"root:/{onedrive_target_path}:").create_upload_session(upload_session.upload_url).upload(file, file_size) upload_task.wait() print(f"File uploaded successfully to OneDrive: /{onedrive_target_path}") # Example: Upload local "report.pdf" to OneDrive's "Documents" folder upload_file_to_onedrive("report.pdf", "Documents/onedrive_report.pdf")
When you don't have a web app to handle redirects, you need to register your tool as a native/desktop app in Azure AD, and use this standard redirect URI:
https://login.microsoftonline.com/common/oauth2/nativeclient
Why this works:
This URI is designed specifically for public client apps (like command-line tools or desktop apps) that can't host a web server. When using the device code flow (as in the code above), users will see a prompt to open a browser, enter a generated code, and log in—no need for your own web page to handle the redirect.
Quick Azure AD setup tip:
- Go to the Azure Portal > Azure Active Directory > App Registrations
- Create a new app, select "Mobile and desktop applications" as the platform
- Add the redirect URI
https://login.microsoftonline.com/common/oauth2/nativeclientto your app's authentication settings
内容的提问来源于stack exchange,提问作者Boobal Ganesan




