前言

上一篇说到申请office 365 E5的方法。但是他需要调用微软的一些api才能续订。所以我们可以在azure创建应用,来调用他的api。

这里介绍一款Rclone的应用,它可以调用onedrive的接口。

创建 OneDrive API

获取 Client ID

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

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


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

获取 Client secret

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

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

设置 API 权限

点击API 权限,按照图示进行操作,添加Files.ReadFiles.ReadWriteFiles.Read.AllFiles.ReadWrite.Alloffline_accessUser.Read这些权限。


获取 token

在本地电脑上下载 rclone
以 Windows 为例子,解压并进入rclone.exe所在文件夹,在资源管理器地址栏输入cmd,回车就会在当前路径打开命令提示符。
替换以下命令中的Client_IDClient_secret并执行。

1
rclone authorize "onedrive" "Client_ID" "Client_secret"

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

1
2
3
4
5
6
7
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"}整个内容(包含括号)就是 to­ken ,复制并保存好。

Rclone 连接 OneDrive

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
$ 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> 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 / JottaCloud
\ "jottacloud"
17 / Koofr
\ "koofr"
18 / Local Disk
\ "local"
19 / Mail.ru Cloud
\ "mailru"
20 / Mega
\ "mega"
21 / Microsoft Azure Blob Storage
\ "azureblob"
22 / Microsoft OneDrive
\ "onedrive"
23 / OpenDrive
\ "opendrive"
24 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
25 / Pcloud
\ "pcloud"
26 / Put.io
\ "putio"
27 / QingCloud Object Storage
\ "qingstor"
28 / SSH/SFTP Connection
\ "sftp"
29 / Transparently chunk/split large files
\ "chunker"
30 / Union merges the contents of several remotes
\ "union"
31 / Webdav
\ "webdav"
32 / Yandex Disk
\ "yandex"
33 / http Connection
\ "http"
34 / premiumize.me
\ "premiumizeme"
Storage> 22 # 选择 Microsoft OneDrive ,截止2020年2月23日序号为 22
** 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
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://p3terx-my.sharepoint.com/personal/xxxxxx/Documents
Is that okay?
y) Yes
n) No
y/n> y # 问你是否确认,如果没问题则输入 y

--------------------
[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
==== ====
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 退出

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

1
touch test && rclone move test od-e5-api:

挂载为磁盘

首先安装fuse

1
2
3
4
#Debian/Ubuntu系统
apt-get install fuse -y
#CentOS系统
yum install fuse -y
1
2
3
4
#新建本地文件夹,路径自己定,即下面的LocalFolder
mkdir /od
#挂载为磁盘,下面的DriveName、Folder、LocalFolder参数根据说明自行替换
rclone mount DriveName:Folder LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

DriveName为初始化配置填的name,Folder为OneDrive里的文件夹,LocalFolder为VPS上的本地文件夹。

挂载成功后,输入df -h命令查看即可!

开机自启

先新建systemd配置文件,适用CentOS 7Debian 8+Ubuntu 16+

DriveNameRclone的配置名称,比如你配置rclone的时候,Name填的od
Folder为网盘里的文件夹路径,如果你要挂载整个网盘,直接填/挂载整个根目录
LocalFolder为本地服务器上的挂载文件夹,如果你需要将网盘的文件夹挂载到服务器上的/Drive文件夹,那就填/Drive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#以下是一整条命令,先修改DriveName:Folder和LocalFolder(有三处地方)的值,再一起复制到SSH客户端运行

cat > /etc/systemd/system/rclone.service <<EOF
[Unit]
Description=Rclone
AssertPathIsDirectory=LocalFolder
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/rclone mount DriveName:Folder LocalFolder \
--umask 0000 \
--default-permissions \
--allow-non-empty \
--allow-other \
--buffer-size 32M \
--dir-cache-time 12h \
--vfs-read-chunk-size 64M \
--vfs-read-chunk-size-limit 1G
ExecStop=/bin/fusermount -u LocalFolder
Restart=on-abort
User=root

[Install]
WantedBy=default.target
EOF

开始启动:

1
systemctl start rclone

设置开机自启:

1
systemctl enable rclone

其他命令:

1
2
3
重启:systemctl restart rclone
停止:systemctl stop rclone
状态:systemctl status rclone

至此,Rclone 已成功使用自建 API 连接到了 OneDrive 网盘。如果需要在vps上挂载,你需要在本地获取token,然后再在vps上配置rclone链接OneDrive

Windows下可视化上传下载 RcloneBrowser

windows下可选,非必须,感觉OneDrive自己的客户端都很好用了

RcloneBrowser是基于rclone来实现OneDrive文件的上传下载,个人觉得微软自己的OneDrive客户端还不错没必要用这个.但是如果为了调用API保活,这大概是最简单的方法了

https://github.com/kapitainsky/RcloneBrowser/releases 下载RcloneBrowser

http://www.secfs.net/winfsp/download/ 下载必要插件,这个插件是挂载onedrive到某个盘符要用到的

下载好后进行安装,然后进行配置。配置 rclone.exe的路径还有 rclone.conf 配置文件的路径。

上传文件