记录一下使用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即可

[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"
 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 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
32 / Transparently chunk/split large files
   \ "chunker"
33 / Union merges the contents of several upstream fs
   \ "union"
34 / Webdav
   \ "webdav"
35 / Yandex Disk
   \ "yandex"
36 / http Connection
   \ "http"
37 / premiumize.me
   \ "premiumizeme"
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会一直报错:

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,搞了好久,也在网上查了一下,很多人遇到过这个问题,但是没有看到能用的解决办法,最终也没搞清楚原因,反正在服务器上创建出来了!

评论