4.9 KiB
4.9 KiB
OneDrive作为存储的配置方法
仅支持工作或学校账户,并且需要有管理员权限以授权API
1. 需要配置的参数
file_storage=onedrive
onedrive_domain=XXXXXX
onedrive_client_id=XXXXXX-XXXXXX-XXXXXX-XXXXXX
onedrive_username=XXXXXX@XXXXXX
onedrive_password=XXXXXX
onedrive_username
和onedrive_password
是你的账户名(邮箱)和密码,另外两个参数需要在微软Azure门户中注册应用后获取。
2. 应用注册
-
登录https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade,鼠标置于右上角账号处,浮窗将显示的
域
即为onedrive_domain
的值。 -
点击左上角的
+新注册
,输入名称,
- 受支持的帐户类型:选择任何组织目录(任何 Azure AD 目录 - 多租户)中的帐户和个人 Microsoft 帐户(例如,Skype、Xbox)
- 重定向 URI (可选):选择
Web
,并输入http://localhost
- 在左侧选择
身份验证
,找到允许的客户端流
,选择是
,并点击保存
。 - 在左侧选择
API权限
,点击+添加权限
,选择Microsoft Graph
->委托的权限
,并勾选下述权限:openid、Files中所有权限、User.Read,如下图所示。最后点击下方的添加权限
。 - 最后点击
授予管理员同意
,并点击是
,最终状态变为已授予
。
3. 使用下述代码测试是否配置成功
安装依赖:pip install Office365-REST-Python-Client
# common.py
import msal
domain = 'XXXXXX'
client_id = 'XXXXXX'
username = 'XXXXXX'
password = 'XXXXXX'
def acquire_token_pwd():
authority_url = f'https://login.microsoftonline.com/{domain}'
app = msal.PublicClientApplication(
authority=authority_url,
client_id=client_id
)
result = app.acquire_token_by_username_password(
username=username,
password=password,
scopes=['https://graph.microsoft.com/.default']
)
return result
测试登录,如果成功打印出账户名,说明配置成功。
from common import acquire_token_pwd
from office365.graph_client import GraphClient
try:
client = GraphClient(acquire_token_pwd)
me = client.me.get().execute_query()
print(me.user_principal_name)
except Exception as e:
print(e)
测试文件上传
import os
from office365.graph_client import GraphClient
from common import acquire_token_pwd
remote_path = 'tmp'
local_path = '.tmp/1689843925000.png'
def convert_link_to_download_link(link):
import re
p1 = re.search(r'https:\/\/(.+)\.sharepoint\.com', link).group(1)
p2 = re.search(r'personal\/(.+)\/', link).group(1)
p3 = re.search(rf'{p2}\/(.+)', link).group(1)
return f'https://{p1}.sharepoint.com/personal/{p2}/_layouts/52/download.aspx?share={p3}'
client = GraphClient(acquire_token_pwd)
folder = client.me.drive.root.get_by_path(remote_path)
# 1. upload
file = folder.upload_file(local_path).execute_query()
print(f'File {file.web_url} has been uploaded')
# 2. create sharing link
remote_file = folder.get_by_path(os.path.basename(local_path))
permission = remote_file.create_link("view", "anonymous").execute_query()
print(f"sharing link: {convert_link_to_download_link(permission.link.webUrl)}")
测试文件下载
import os
from office365.graph_client import GraphClient
from common import acquire_token_pwd
remote_path = 'tmp/1689843925000.png'
local_path = '.tmp'
if not os.path.exists(local_path):
os.makedirs(local_path)
client = GraphClient(acquire_token_pwd)
remote_file = client.me.drive.root.get_by_path(remote_path).get().execute_query()
with open(os.path.join(local_path, os.path.basename(remote_path)), 'wb') as local_file:
remote_file.download(local_file).execute_query()
print(f'{remote_file.name} has been downloaded into {local_file.name}')
测试删除文件
from office365.graph_client import GraphClient
from common import acquire_token_pwd
remote_path = 'tmp/1689843925000.png'
client = GraphClient(acquire_token_pwd)
file = client.me.drive.root.get_by_path(remote_path)
file.delete_object().execute_query()