Rclone 使用自建 API 连接 OneDrive(Office 365 E5 自动续订保活)

  1. 前言
  2. 速度测试
  3. 创建 OneDrive API
    1. 获取 Client ID
    2. 获取 Client secret
    3. 设置 API 权限
  4. 获取 token
  5. Rclone 连接 OneDrive

前言

使用 Rclone 给 OneDrive 传输文件时可能会遇到速度非常慢、断联等一些问题,其根源是 OneDrive 使用 Rclone 自带 API 会有一些限制,毕竟是很多人都在共用一个 API 接口。使用自建的 API 连接 OneDrive 可大幅改善这些情况,而且对于三个月的 Office 365 E5 开发者试用版,平时偶尔用用 Rclone 还会有自动续订的可能性,比起刻意刷 API 的方式更为安全和稳定。此外自建的 API 还可以给其它的账号和其它应用使用。

速度测试

前者为使用 Rclone 自带 API ,后者为自建 API ,可以很明显的看出速度相差了 10 多倍。

WEBRESOURCE969427b22d72bd0c820bfdf3f36da870.png

WEBRESOURCEdf8d9c1d4b23e9b5ab7da4118c5802f3.png

创建 OneDrive API

获取 Client ID

进入 Microsoft Azure 应用注册页面。点击新注册。

https://entra.microsoft.com/

WEBRESOURCE083b26b736363e12e39cec6e55c3f8d7.png

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

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

WEBRESOURCE7299c6ea3b78aac7a9e26a3db340c277.png

WEBRESOURCEf51b37c105232630af83fa7dcf02fb04.png

获取 Client secret

点击证书和密码,按照图中序号的顺序操作添加密码。

WEBRESOURCE97c7344cebc4b9fb4f11b35f5c01cb68.png

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

WEBRESOURCEe0f2427e228d06def723958a058236d1.png

设置 API 权限

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

WEBRESOURCE68611155292002ea922c1c5a4391257c.png

最后确认下权限是否添加完整。

WEBRESOURCE8d22b422774f4695cd271ecb910b7dcc.png

获取 token

在本地电脑上下载 rclone。

飘云网盘下载:http://piaoyun.online/电脑软件/上传下载/rclone

官网下载:https://rclone.org/downloads/

GitHub下载:https://github.com/ncw/rclone

另外Windows使用rclone还需要另一个依赖工具winfsp,下载地址:http://www.secfs.net/winfsp/download/

以 Windows 为例子,解压并进入rclone.exe所在文件夹,在资源管理器地址栏输入cmd,回车就会在当前路径打开命令提示符。

替换以下命令中的Client_ID、Client_secret并执行。

rclone authorize "onedrive" "Client_ID" "Client_secret"

Bash

Copy

接下来会弹出浏览器,要求你登录账号进行授权。授权完后命令提示符窗口会出现以下信息:

If your browser doesn't open automatically go to the following link: <http://10.10.10.10: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

Bash

Copy

{“access_token”:”xxxxxxxxxxxxxxxxxx”,”expiry”:”2020-02-22T21:18:39.5036298+08:00”} 整个内容(包含括号)就是 to-ken ,复制并保存好。

Rclone 连接 OneDrive

输入 rclone config 命令,会出现以下信息,参照下面的注释进行操作。

TIPS: 因为 RCLONE 会时不时进行更新,当你看到这篇教程时菜单选项可能已经发生了略微的改动,但大致思路不会变,不要无脑照搬操作。

$ rclone config

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 # 输入 n,新建
name> piaoyun.cc-od-e5-api # 输入网盘名称,类似于标签,用于区分不同的网盘。
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 Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)
   \\ "s3"
 5 / Backblaze B2
   \\ "b2"
 6 / Box
   \\ "box"
 7 / Cache a remote
   \\ "cache"
 8 / Citrix Sharefile
   \\ "sharefile"
 9 / Dropbox
   \\ "dropbox"
10 / Encrypt/Decrypt a remote
   \\ "crypt"
11 / FTP Connection
   \\ "ftp"
12 / Google Cloud Storage (this is not Google Drive)
   \\ "google cloud storage"
13 / Google Drive
   \\ "drive"
14 / Google Photos
   \\ "google photos"
15 / Hubic
   \\ "hubic"
16 / In memory object storage system.
   \\ "memory"
17 / JottaCloud
   \\ "jottacloud"
18 / Koofr
   \\ "koofr"
19 / Local Disk
   \\ "local"
20 / Mail.ru Cloud
   \\ "mailru"
21 / Mega
   \\ "mega"
22 / Microsoft Azure Blob Storage
   \\ "azureblob"
23 / Microsoft OneDrive
   \\ "onedrive"
24 / OpenDrive
   \\ "opendrive"
25 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \\ "swift"
26 / Pcloud
   \\ "pcloud"
27 / Put.io
   \\ "putio"
28 / QingCloud Object Storage
   \\ "qingstor"
29 / SSH/SFTP Connection
   \\ "sftp"
30 / Sugarsync
   \\ "sugarsync"
31 / Transparently chunk/split large files
   \\ "chunker"
32 / Union merges the contents of several remotes
   \\ "union"
33 / Webdav
   \\ "webdav"
34 / Yandex Disk
   \\ "yandex"
35 / http Connection
   \\ "http"
36 / premiumize.me
   \\ "premiumizeme"
Storage> 23 # 选择 Microsoft OneDrive ,截止2020年5月13日序号为 23
** See help for onedrive backend at: <https://rclone.org/onedrive/> **

Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> # 输入前面记录下来的 Client Id (客户端 ID)
Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> # 输入前面记录下来的 Client Secret (客户端密码)
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n # 输入 n
Remote config
Make sure your Redirect URL is set to "<http://localhost:53682/>" in your custom config.
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> n # 输入 n
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine (same rclone version recommended) :
    rclone authorize "onedrive" "client_id" "client_secret"
Then paste the result below:
result> {"access_token":"XXXXXXXXX","expiry":"2020-02-22T21:18:39.5036298+08:00"}   # 输入 token,前面生成记录下来的token
Choose a number from below, or type in an existing value
 1 / OneDrive Personal or Business
   \\ "onedrive"
 2 / Root Sharepoint site
   \\ "sharepoint"
 3 / Type in driveID
   \\ "driveid"
 4 / Type in SiteID
   \\ "siteid"
 5 / Search a Sharepoint site
   \\ "search"
Your choice> 1 # 这里问你要选择的类型,因为是使用 OneDrive ,所以输入 1
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=xxxxxxxxxxxxxx
Chose drive to use:> 0 # 检测到了网盘,这里编号是 0 ,所以输入 0
Found drive 'root' of type 'business', URL: <https://piaoyun.cc-my.sharepoint.com/personal/piaoyun.cc/Documents>
Is that okay?
y) Yes
n) No
y/n> y # 问你是否确认,如果没问题则输入 y

--------------------
[piaoyun.cc-od-e5-api]
type = onedrive
client_id = xxxxxxxxxx
client_secret = xxxxxxxxxxxxxxxx
token = {"access_token":"xxxxxxxxxxxxxxxxxx","expiry":"2020-02-22T21:18:39.5036298+08:00"}
drive_id = xxxxxxxxxxxxxxxxxxxxxxxxx
drive_type = business
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y # 最后会显示网盘的配置信息并询问你是否正确,如果没问题则输入 y
Current remotes:

Name                 Type
====                 ====
piaoyun.cc-od-e5-api            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 # 输入 q 退出

Bash

Copy

最后验证一下是否能正常使用,替换以下命令中的 od-e5-api 并执行,注意最后有:(冒号)。网盘中出现 test 文件即表示成功。

touch test && rclone move test piaoyun.cc-od-e5-api:

Bash

Copy

至此,Rclone 已成功使用自建 API 连接到了 OneDrive 网盘。

延伸阅读:

使用软件rclone在Windows操作系统上挂载OneDrive为本地硬盘的操作方法:

https://piaoyun.cc/1290.html

参考资料:

Rclone 使用自建 API 连接 OneDrive(Office 365 E5 自动续订):

https://p3terx.com/archives/rclone-connect-onedrive-with-selfbuilt-api.html

Rclone Documentation – Microsoft OneDrive:https://rclone.org/onedrive/


欢迎指出任何有错误或不够清晰的表达,可以在下面评论区评论。

×

喜欢就点赞,疼爱就打赏

//