sent-file-fast-serve/readme_onedrive.md
2024-11-30 19:49:52 +08:00

4.9 KiB
Raw Blame History

OneDrive作为存储的配置方法

仅支持工作或学校账户并且需要有管理员权限以授权API

1. 需要配置的参数

file_storage=onedrive
onedrive_domain=XXXXXX
onedrive_client_id=XXXXXX-XXXXXX-XXXXXX-XXXXXX
onedrive_username=XXXXXX@XXXXXX
onedrive_password=XXXXXX

onedrive_usernameonedrive_password是你的账户名(邮箱)和密码,另外两个参数需要在微软Azure门户中注册应用后获取。

2. 应用注册

  1. 登录https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade,鼠标置于右上角账号处,浮窗将显示的即为onedrive_domain的值。 onedrive_domain

  2. 点击左上角的+新注册,输入名称,

  • 受支持的帐户类型:选择任何组织目录(任何 Azure AD 目录 - 多租户)中的帐户和个人 Microsoft 帐户(例如Skype、Xbox)
  • 重定向 URI (可选):选择Web,并输入http://localhost
  1. 完成注册后进入概述页面,在概要中找到应用程序(客户端)ID,即为onedrive_client_id的值。 onedrive_client_id

  2. 此时还需要配置允许公共客户端流和API权限

  • 在左侧选择身份验证,找到允许的客户端流,选择,并点击保存允许的客户端流
  • 在左侧选择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()