python上传文件到onedrive_使用Python从个人OneDrive下载文件

本文介绍了一种通过Python脚本实现自动从个人OneDrive下载和上传文件的方法。利用Microsoft Graph API,创建Azure AD应用,配置权限,并通过刷新令牌实现无用户交互的持续同步。文中详细阐述了配置过程和Python实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a Python script that is running periodically on an AWS EC2 Ubuntu machine.

This script reads data from some files and sometimes changes data in them.

I want to download these files from OneDrive, do my own thing with them, and upload them back to OneDrive.

I want this to be done automatically, without the need for a user to approve any login or credentials. I'm ok with doing it once (i.e. approving the login on the first run) but the rest has to run automatically, without asking ever again for approvals (unless the permissions change, of course).

What is the best way to do this?

I've been reading the documentation on Microsoft Graph API but I'm struggling with the authentication part. I've created an application in Azure AAD, gave the sample permissions (to test) and created a secret credential.

解决方案

I managed to do it. I'm not sure if it's the best way but it is working now. It's running automatically every hour and I don't need to touch it.

This is what I did.

Azure Portal

Create an application. Azure Active Directory -> App Registrations -> Applications from personal account

In Supported account types, choose the one that has personal Microsoft accounts.

In Redirect URI, choose Public client/native. We'll add the specific URI later.

In the application details, in the section Overview, take note of the Application (client) ID. We'll need this later.

In the section Authentication, click Add a Platform and choose Desktop + devices. You can use your own, I chose one of the suggested: https://login.microsoftonline.com/common/oauth2/nativeclient

In the section API permissions, you have to add all the permissions that your app will use. I added User.Read, Files.ReadWrite and offline_access. The offline_access is to be able to get the refresh token, which will be crucial to keep the app running without asking the user to login.

I did not create any Certificate or Secret.

Web

Looks like to get a token for the first time we have to use a browser or emulate something like that.

There must be a programmatic way to do this, but I had no idea how to do it. I also thought about using Selenium for this, but since it's only one time and my app will request tokens every hour (keeping the tokens fresh), I dropped that idea.

If we add new permissions, the tokens that we have will become invalid and we have to do this manual part again.

Open a browser and go to the URL below. Use the Scopes and the Redirect URI that you set up in Azure Portal.

That URL will redirect you to the Redirect URI that you set up and with a code=something in the URL. Copy that something.

Do a POST request with type FORM URL Encoded. I used https://reqbin.com/ for this.

Form URL: grant_type=authorization_code&client_id=your_app_client_id&code=use_the_code_returned_on_previous_step

This will return an Access Token and a Refresh Token. Store the Refresh Token somewhere. I'm saving it in a file.

Python

# Build the POST parameters

params = {

'grant_type': 'refresh_token',

'client_id': your_app_client_id,

'refresh_token': refresh_token_that_you_got_in_the_previous_step

}

response = requests.post('https://login.microsoftonline.com/common/oauth2/v2.0/token', data=params)

access_token = response.json()['access_token']

new_refresh_token = response.json()['refresh_token']

# ^ Save somewhere the new refresh token.

# I just overwrite the file with the new one.

# This new one will be used next time.

header = {'Authorization': 'Bearer ' + access_token}

# Download the file

response = requests.get('https://graph.microsoft.com/v1.0/me/drive/root:' +

PATH_TO_FILE + '/' + FILE_NAME + ':/content', headers=header)

# Save the file in the disk

with open(file_name, 'wb') as file:

file.write(response.content)

So basically, I have the Refresh Token always updated.

I call the Token endpoint using that Refresh Token, and the API gives me an Access Token to use during the current session and a new Refresh Token.

I use this new Refresh Token the next time I run the program, and so on.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值