记录一下使用Rclone获取google drive token的过程

简介

概述

Rclone 是一款的命令行工具,支持在不同对象存储、网盘间同步、上传、下载数据。

挂载网盘

挂载网盘分为手动挂载和开机自动挂载,根据自己的需求来选择。

手动挂载

1
2
3
4
5
#挂载
rclone mount <网盘名称:网盘路径> <本地路径> [参数] --daemon

#取消挂载
fusermount -qzu <本地路径>

网盘名称为配置时填的 name,网盘路径为网盘里的文件夹,留空为整个网盘,本地路径为 VPS 上的本地文件夹。参数可以查看官方文档根据需求进行选择。实际输入时不要有括号,这里只是为了更清楚的区分。–daemon 为进程守护参数,可后台运行。

挂载只要几秒钟,有时终端不会返回成功信息,关闭 SSH 重连即可,不能Ctrl+C;重连后查看是否挂载成功:df -h

所以最好使用nohup执行命令,ssh断开了,也不影响未完的命令执行!

复制

rclone copy时,目的路径如果不存在会逐层创建

通过Rclone获取Google Drive Token

说明

如果你是首次来到google开发者中心,需要先创建一个“项目”,然户创建“OAuth 同意屏幕”,才能执行其他操作!

开启Google Drive API

去这里开启Google Drive API:
https://console.developers.google.com/apis/api/drive.googleapis.com/overview

创建一个 OAuth client ID

去这里创建OAuth client ID:
https://console.developers.google.com/apis/credentials/oauthclient

注意这里的应用类型选择“桌面设备”!

本地安装rclone

去rclone官网https://rclone.org/downloads/,按设备类型下载安装对应版本即可 ;

建议在linux服务器上操作,Windows上我没成功,耽误了很多时间!

使用rclone获取refresh_token

直接执行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
[root@instance-4 ~]# rclone config
2020/06/07 08:36:35 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n #输入n,表示新建
name> goindex
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"
...
12 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
13 / Google Drive
\ "drive"
14 / Google Photos
\ "google photos"
...

38 / seafile
\ "seafile"
Storage> 13 #按照需要,输入上述列表中的编号,这里输入13,也可以直接输入引号中的字符串
** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> 922792137230-p1hacd0v1r53n2rn34p39cuquut449b6.apps.googleusercontent.com
Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret> 3SDM_QN36HqgdYIYD0cs1zpQ
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
1 / Full access all files, excluding Application Data Folder.
\ "drive"
2 / Read-only access to file metadata and file contents.
\ "drive.readonly"
/ Access to files created by rclone only.
3 | These are visible in the drive website.
| File authorization is revoked when the user deauthorizes the app.
\ "drive.file"
/ Allows read and write access to the Application Data folder.
4 | This is not visible in the drive website.
\ "drive.appfolder"
/ Allows read-only access to file metadata but
5 | does not allow any access to read or download file content.
\ "drive.metadata.readonly"
scope> 1 #输入1,表示全部权限
ID of the root folder
Leave blank normally.

Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.

Note that if this is blank, the first time rclone runs it will fill it
in with the ID of the root folder.

Enter a string value. Press Enter for the default ("").
root_folder_id>
Service Account Credentials JSON file path
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file>
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n #输入n
Remote config
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 #输入n;如果是在服务器上,这里输入y之后,会给你一个127.0.0.1的网址让你访问授权,这样就无法实现,所以这里选n,会给你一个通用链接,可以在本地电脑上访问授权;
Please go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=922792137230-p1hacd0v1r53n2rn34p39cuquut449b6.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=0NMrhpEREX28r25plm3ZwA
Log in and authorize rclone for access
Enter verification code> 4/0gGbQNwEc40VPc1edE-5XuOl1Ycm0oK1_Gj4OsvTYE3LshQS1eIoZDk
Configure this as a team drive?
y) Yes
n) No (default)
y/n> #不是团队盘,就输入n即可
--------------------
[goindex]
type = drive
client_id = xxx
client_secret = xxx
scope = drive
token = xxx
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y #输入y确认
Current remotes:

Name Type
==== ====
goindex drive

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退出
[root@instance-4 ~]#

注意,这里有个大坑,不要使用Windows,使用Windows执行后面的创建rclone config时,获取token会一直报错:

1
Failed to configure token: failed to get token: Post [https://oauth2.googleapis.com/token 2] read tcp 192.168.0.110:13934->[172.217.27.138:443]: wsarecv: An existing connection was forcibly closed by the remote host

用Windows就是不成功,最后到服务器上跑了下,一点问题都没有!

至此,token获取结束,因为Windows,搞了好久,也在网上查了一下,很多人遇到过这个问题,但是没有看到能用的解决办法,最终也没搞清楚原因,反正在服务器上创建出来了!

Rclone挂载世纪互联OneDrive

这里挂载同济大学edu自带的世纪互联OneDrive 1T

准备工作

准备API

因为同济大学edu带的世纪互联Office365不能进add,也就无法申请API,但是它可以使用世纪互联其他域下的API,所以这里申请一个空的世纪互联全局,去开一个 API即可

申请链接: Office365 教育试用版 (世纪互联) 申请地址

安装Rclone

世纪互联OD需要修改版的Rclone(linux和pc)

修改版下载地址:链接

  • Linux先使用 yum install rclone 安装一遍,然后用上面的修改版替换 /usr/bin/ 下的 rclone
  • Windows下解压出来,直接在 cmd 中使用即可!

操作步骤

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
[root@instance-20200730-1319-centos7 ~]# rclone config
Current remotes:

Name Type
==== ====
tongjiwgq 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> d
Choose a number from below, or type in an existing value
1 > tongjiwgq
remote> 1
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> tongjiwgq
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"
...
22 / Microsoft OneDrive
\ "onedrive"
...
Storage> ondedrive
** 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> 16263263-8cb4-460d-8e16-88a10f888fda
Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> puvL0J24q4vYXA26qm64FJ-5prf.b_6nG-
OneDrive operated by 21Vianet (世纪互联).
Enter a boolean value (true or false). Press Enter for the default ("false").
is_21vianet_version> true
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,因为这里是ssh连接到远端的机器,并不是在本地机器上挂载
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" "16263263-8cb4-460d-8e16-88a10f888fda" "puvL0J24q4vYXA26qm64FJ-5prf.b_6nG-" #这里拼接上 --onedrive-is-21vianet-version=true,到一个带有浏览器环境的机器中(也就是要在Windows机器上执行),用cmd执行这条拼接后的完整命令,会弹出网页认证登录;认证完成之后,cmd窗口中会出现该复制的内容,复制黏贴到ssh窗口中下面result的地方;
Then paste the result below:
result> #上面获取到的内容贴到这里
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 #输入1
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=b!bmHt7CWXlk61AgIYlM-wftJRCTSL4HVOgQaNud522imBdQfaEXNRQbpWi15wd7Ji
Chose drive to use:> 0 #输入0,这里是选择你要使用的盘
Found drive 'root' of type 'business', URL: https://universitytongji-my.sharepoint.cn/personal/wgq_alumni_tongji_edu_cn/Documents
Is that okay?
y) Yes
n) No
y/n> y #输入y
2020/08/24 23:02:17 ERROR : Failed saving config "drive_id" = "b!bmHt7CWXlk61AgIYlM-wftJRCTSL4HVOgQaNud522imBdQfaEXNRQbpWi15wd7Ji" in section "tongjiwgq" of the config file: section 'tongjiwgq' not found
2020/08/24 23:02:17 ERROR : Failed saving config "drive_type" = "business" in section "tongjiwgq" of the config file: section 'tongjiwgq' not found
--------------------
[tongjiwgq] #全部确认完毕后,这里会出现本盘的 所有相关信息,隐私信息用星号代替隐藏
type = onedrive
client_id = ***
client_secret = ***
is_21vianet_version = true
token = ***
drive_id = ***
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
==== ====
tongjiwgq 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 #退出配置

遇到的问题

去宝塔创建守护进程之后,启动失败,将挂载命令直接在ssh shell窗口中执行:

1
rclone mount tongjiwgq: /cloud/tongjiwgq --umask 0000 --default-permissions --allow-non-empty --allow-other --transfers 8 --buffer-size 256M --low-level-retries 200 --dir-cache-time 12h --vfs-read-chunk-size 256M --vfs-read-chunk-size-limit 3G --daemon

报错:

1
Fatal error: failed to mount FUSE fs: fusermount: exec: "fusermount": executable file not found in $PATH

是因为没有安装fuse,执行yum install fuse安装之后,再次执行依然报错:

1
Fatal error: failed to mount FUSE fs: fusermount: exit status 1

是因为我设置的挂载点的文件夹并没有创建出来,手动创建出来之后,再去mount即可!

然后在宝塔中创建守护进程,启动命令设置为:

1
rclone mount tongjiwgq: /cloud/tongjiwgq --umask 0000 --default-permissions --allow-non-empty --allow-other --transfers 8 --buffer-size 256M --low-level-retries 200 --dir-cache-time 12h --vfs-read-chunk-size 256M --vfs-read-chunk-size-limit 3G

其中的一些参数取值根据自己的需求去配置!

注意

  • Linux中替换后的 rclone 程序记得给执行权限

    chmod +x /usr/bin/rclone

  • 在cmd中执行拼接后的命令之前,确保世纪互联官网没有登录账号,或者登录的账号是你需要挂载的账号,否则会挂错盘!
  • 在挂载之前,确保挂载点的目录已经存在;

    mkdir -p /path/path

  • rclone mount后面跟的tongjiwgq是配置名称,是创建配置时自己输入的!

参考

Rclone挂载Google 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
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
[root@instance-20200730-1319-centos7 ~]# rclone config
Current remotes:

Name Type
==== ====
tongjiwgq 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> n
name> albanywgq
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"
...
13 / Google Drive
\ "drive"
14 / Google Photos
\ "google photos"
...
34 / premiumize.me
\ "premiumizeme"
Storage> 13
** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id>
Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret>
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
1 / Full access all files, excluding Application Data Folder.
\ "drive"
2 / Read-only access to file metadata and file contents.
\ "drive.readonly"
/ Access to files created by rclone only.
3 | These are visible in the drive website.
| File authorization is revoked when the user deauthorizes the app.
\ "drive.file"
/ Allows read and write access to the Application Data folder.
4 | This is not visible in the drive website.
\ "drive.appfolder"
/ Allows read-only access to file metadata but
5 | does not allow any access to read or download file content.
\ "drive.metadata.readonly"
scope> drive
ID of the root folder
Leave blank normally.

Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.

Note that if this is blank, the first time rclone runs it will fill it
in with the ID of the root folder.

Enter a string value. Press Enter for the default ("").
root_folder_id>
Service Account Credentials JSON file path
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file>
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n
Remote 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,因为在远端机器,然后会给你一个链接
If your browser doesnt open automatically go to the following link: https:***(隐私隐藏)
Log in and authorize rclone for access #粘贴此链接到浏览器无痕模式
Enter verification code> 4/3QF5QkEh5jMOJ0khErJ_6ZLZQhGVbvCk8t81vmbIR58lGaLhHenbHzk #将登录得到的 code贴到这里
Configure this as a team drive?
y) Yes
n) No
y/n> n
--------------------
[albanywgq]
type = drive
scope = drive
token = ***
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

Name Type
==== ====
albanywgq drive
tongjiwgq 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
[root@instance-20200730-1319-centos7 ~]#

挂载

1
rclone mount albanywgq: /cloud/albanywgq  --daemon

遇到的问题

1
2
3
4
5
6
7
8
9
# 没带参数执行了一遍
[root@instance-20200730-1319-centos7 ~]# rclone mount albanywgq: /cloud/albanywgq --daemon
2020/08/25 01:27:37 Fatal error: Can not open: /cloud/albanywgq: open /cloud/albanywgq: transport endpoint is not connected

# 此时再把参数带上
[root@instance-20200730-1319-centos7 /cloud]# rclone mount albanywgq: /cloud/albanywgq/ --umask 0000 --default-permissions --allow-non-empty --allow-other --transfers 8 --buffer-size 256M --low-level-retries 200 --dir-cache-time 12h --vfs
-read-chunk-size 256M --vfs-read-chunk-size-limit 3G --daemon
2020/08/25 01:29:59 mount helper error: fusermount: failed to access mountpoint /cloud/albanywgq: Transport endpoint is not connected
2020/08/25 01:29:59 Fatal error: failed to mount FUSE fs: fusermount: exit status 1

法一

大概意思是挂载点权限不足哦,原来刚开始挂载时没有通过参数指定权限,导致后面出现这些问题,所以挂载时一定要通过参数指定下权限;

这里重新创建一个文件夹,然后带参数重新执行一遍即可

所以第一次挂载一定要指定挂载点的权限啊,不过万一忘记了,遇到这种问题了,也无需删除配置重建,直接新建个文件夹,换个挂载点再挂载一次即可!

正确的权限应该是777

正确命令如下(创建一个新的文件夹albanywgq1作为新的挂载点即可):

1
2
rclone mount albanywgq: /cloud/albanywgq1/ --umask 0000 --default-permissions --allow-non-empty --allow-other --transfers 8 --buffer-size 256M --low-level-retries 200 --dir-cache-time 12h --vfs
-read-chunk-size 256M --vfs-read-chunk-size-limit 3G --daemon

法二(亲测有效)

也有可能是多次挂载造成的,使用

1
ps aux | grep rclone

明显能够看到,一个挂载人去,明显有多个进程

执行

1
fusermount -qzu  <挂载点>

将多余的挂载取消掉

并且一个盘到目前为止被同时挂载到了两个挂载点

参考

参考

评论