前言
使用 Rclone 给 OneDrive 传输文件时可能会遇到速度非常慢、断联等一些问题,其根源是触发了 OneDrive API 的限制,而默认的 Rclone 内置 API 由于非常多人在同时在使用所以这些问题也就愈发明显。使用自建的私有 API 连接 OneDrive 可大幅改善这些情况,而且对于三个月的 Office 365 E5 开发者试用版,平时偶尔用用 Rclone 还会有自动续订的可能性,比起刻意刷 API 的方式更为安全和稳定。此外自建的 API 还可以给其它的账号和其它应用使用。
速度测试
前者为使用 Rclone 自带 API ,后者为自建 API ,可以很明显的看出速度相差了 10 多倍。


创建 OneDrive API
获取 Client ID

- 名称随意,账户类型选择最后一个,重定向 URL 填写
http://localhost
。

- 创建成功后你会看到 Client ID(客户端 ID),复制并保存好。

获取 Client secret

- 然后你会看到 Client secret(客户端密码),复制并保存好。

设置 API 权限
- 点击
API 权限
,按照图示进行操作,添加Files.Read
、Files.ReadWrite
、Files.Read.All
、Files.ReadWrite.All
、offline_access
、User.Read
这些权限。


获取 token
- 在本地电脑上下载 rclone。
- 以 Windows 为例子,解压并进入
rclone.exe
所在文件夹,在资源管理器地址栏输入cmd
,回车就会在当前路径打开命令提示符。
- 替换以下命令中的
Client_ID
、Client_secret
并执行。
rclone authorize "onedrive" "Client_ID" "Client_secret"
接下来会弹出浏览器,要求你登录账号进行授权。授权完后命令提示符窗口会出现以下信息:
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth
Log in and authorize rclone for access
Waiting for code...
Got code
Paste the following into your remote machine --->
{"access_token":"xxxxxxxxxxxxxxxxxx","expiry":"2020-02-22T21:18:39.5036298+08:00"}
<---End paste
{"access_token":"xxxxxxxxxxxxxxxxxx","expiry":"2020-02-22T21:18:39.5036298+08:00"}
整个内容(包含括号)就是 token ,复制并保存好。
Rclone 连接 OneDrive
输入 rclone config
命令,会出现以下信息,参照下面的注释进行操作。
TIPS: 因为 RCLONE 会时不时进行更新,当你看到这篇教程时菜单选项可能已经发生了略微的改动,但大致思路不会变,不要无脑照搬操作。
e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> n
name> onedrive
Option Storage.
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value.
1 / 1Fichier
\ "fichier"
2 / Alias for an existing remote
\ "alias"
3 / Amazon Drive
\ "amazon cloud drive"
4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, SeaweedFS, and Tencent COS
\ "s3"
5 / Backblaze B2
\ "b2"
6 / Better checksums for other remotes
\ "hasher"
7 / Box
\ "box"
8 / Cache a remote
\ "cache"
9 / Citrix Sharefile
\ "sharefile"
10 / Compress a remote
\ "compress"
11 / Dropbox
\ "dropbox"
12 / Encrypt/Decrypt a remote
\ "crypt"
13 / Enterprise File Fabric
\ "filefabric"
14 / FTP Connection
\ "ftp"
15 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
16 / Google Drive
\ "drive"
17 / Google Photos
\ "google photos"
18 / Hadoop distributed file system
\ "hdfs"
19 / Hubic
\ "hubic"
20 / In memory object storage system.
\ "memory"
21 / Jottacloud
\ "jottacloud"
22 / Koofr
\ "koofr"
23 / Local Disk
\ "local"
24 / Mail.ru Cloud
\ "mailru"
25 / Mega
\ "mega"
26 / Microsoft Azure Blob Storage
\ "azureblob"
27 / Microsoft OneDrive
\ "onedrive"
28 / OpenDrive
\ "opendrive"
29 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
30 / Pcloud
\ "pcloud"
31 / Put.io
\ "putio"
32 / QingCloud Object Storage
\ "qingstor"
33 / SSH/SFTP Connection
\ "sftp"
34 / Sia Decentralized Cloud
\ "sia"
35 / Sugarsync
\ "sugarsync"
36 / Tardigrade Decentralized Cloud Storage
\ "tardigrade"
37 / Transparently chunk/split large files
\ "chunker"
38 / Union merges the contents of several upstream fs
\ "union"
39 / Uptobox
\ "uptobox"
40 / Webdav
\ "webdav"
41 / Yandex Disk
\ "yandex"
42 / Zoho
\ "zoho"
43 / http Connection
\ "http"
44 / premiumize.me
\ "premiumizeme"
45 / seafile
\ "seafile"
Storage> 27
Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> //输入client_id
Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> //输入client_secret
Option region.
Choose national cloud region for OneDrive.
Enter a string value. Press Enter for the default ("global").
Choose a number from below, or type in your own value.
1 / Microsoft Cloud Global
\ "global"
2 / Microsoft Cloud for US Government
\ "us"
3 / Microsoft Cloud Germany
\ "de"
4 / Azure and Office 365 operated by 21Vianet in China
\ "cn"
region> 1
Edit advanced config?
y) Yes
n) No (default)
y/n> n
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine
y) Yes (default)
n) No
y/n> n
Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
rclone authorize "onedrive" "XXXXXX-iLCJjbGllbnRfc2VjcmV0IjoiOHhIN1F+VDNUZWVIaUt3aVFocE01dkU3SmRNYk5NSVpNZzhNbiJ9"
Then paste the result.
Enter a string value. Press Enter for the default ("").
config_token> //输入token
Option config_type.
Type of connection
Enter a string value. Press Enter for the default ("onedrive").
Choose a number from below, or type in an existing value.
1 / OneDrive Personal or Business
\ "onedrive"
2 / Root Sharepoint site
\ "sharepoint"
/ Sharepoint site name or URL
3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite
\ "url"
4 / Search for a Sharepoint site
\ "search"
5 / Type in driveID (advanced)
\ "driveid"
6 / Type in SiteID (advanced)
\ "siteid"
/ Sharepoint server-relative path (advanced)
7 | E.g. /teams/hr
\ "path"
config_type> 1
Drive OK?
Found drive "root" of type "business"
URL: https://niepan-my.sharepoint.com/personal/XXXXXX_onmicrosoft_com/Documents
y) Yes (default)
n) No
y/n> y
--------------------
[onedrive]
type = onedrive
client_id = XXXXX
client_secret = XXXXXX
token = XXXXXX
drive_id = b!OcVBXyAeZ0W3eKbndiGmrdtwLB5InRJJiejWXXXXXXXXXX
drive_type = business
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:
Name Type
==== ====
gd drive
onedrive onedrive
e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q
挂载 OneDrive
如果你需要挂载到本地目录,可以参考博主之前写的挂载教程,这里就不再赘述了。
其它说明
私有 API 局限性
自建私有 API 虽然能改善上传体验,但如果使用过于频繁还是会上传受限。
OneDrive API 限制阈值是多少?
微软没有明确说明 OneDrive API 的限制,以下为官方文档原文:
根据使用情况,我们会对阈值进行微调,以便用户可以使用最大数量的资源,而不会降低可靠性和性能。
通过查看其它类型的 API 限制说明可以猜测到的是有总次数和频率这个两种限制。总次数是指一天内所能调用的次数,而频率为每分钟所能调用的次数。而一旦达到限制阈值,那么就会限制文件的上传。
既然无法从官方文档中获得准确数值,那么是否能通过实际测试来获得这个准确数值呢?答案是否定的。在实际测试中并没有发现任何规律,所以说是这个限制是动态调节的,也符合官方文档的说明。
如何避免 OneDrive API 受限?
不要在短时间内上传过多的文件,文件大小无所谓,关键在于文件数量。
关于 Office 365 E5 自动续订
根据博主几年的开发者试用版使用经验来看,只要你使用自建的私有 API 就可能续订,至于频率并没有一个标准,也并不是越多越好。刻意刷 API 可能得不偿失,尤其是使用 GitHub Actions ,因为服务器是微软 Azure 的,那么多人以相似的方式无意义的刷 API 想要识别是非常容易的。
参考资料
Rclone Documentation - Microsoft OneDrive
Microsoft Graph 限制指南
VIA:https://p3terx.com/archives/rclone-connect-onedrive-with-selfbuilt-api.html