Skip to main content

文档|创建Google OAuth

在使用CloudDrive挂载Google Drive时,通过网页认证,会出现此应用已被阻止的问题。所以需要第二种方式,自己申请Google OAuth。此方式也同样也适用于Rclone挂载。

应用已被阻止

CloudDrive使用RefreshToken登录,需要有ClientIdClientSecretRefreshToken。其中ClientId、ClientSecret需要在Google Cloud Platform创建OAuth认证,RefreshToken需要调用接口获取。

创建OAuth认证

1.前往Google API

2.创建项目

创建项目 输入项目名称

等待项目创建完成,选择项目

3.配置权限请求页面

配置权限请求页面 开始 应用信息 受众群体 联系人 点击创建。

4.添加测试账号(没有添加不能登录)

添加测试账号

5.回到Google API

如果这个项目没有启用Google Drive API,会提示启用。

启用

6.创建OAuth凭据

创建OAuth

应用类型和应用名称随便填写,重定向URL按需求填写,可以填http://127.0.0.1,后面需要用上,点击创建。 创建

复制客户端 ID客户端密钥,点击完成。 创建完成

获取RefreshToken

1.获取授权码

client_idredirect_urihttp://127.0.0.1)填写上面获取到的。scope是Google Drive的访问权限,https://www.googleapis.com/auth/drive 包含用户的所有权限。

https://accounts.google.com/o/oauth2/auth?
response_type=code&
client_id={client_id}&
redirect_uri={redirect_uri}&
scope=https://www.googleapis.com/auth/drive&
access_type=offline&
prompt=consent

复制链接在浏览器中打开,选择Google账号,必须是上面添加的测试账号,否则无法登录。如果提示测试环境,点击继续即可。如果提示重定向地址不正确,检查一下redirect_uri是否正确。

登录成功后,会重定向到redirect_uri

http://127.0.0.1/?code=4/0AUJR-x5CVM0xajQxXT-wKvvoa3oUjHcCT9tXyKUZLrnNgfSR5G9-BUYpZwMsR8Ua5nMw&scope=https://www.googleapis.com/auth/drive

获取到code,复制下来。

2.获取RefreshToken

替换下面命令中的{code}{client_id},{client_secret}{redirect_uri},执行命令。

curl -X "POST" "https://oauth2.googleapis.com/token?code={code}&client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}&grant_type=authorization_code" \
-H 'Content-Type: application/x-www-form-urlencoded'

返回结果中有refresh_token,复制下来。

{
"access_token": "ya29.a0AW4XtxhoyMG566cBv76sxE48O_t...",
"expires_in": 3599,
"refresh_token": "1//06WWNolWqcJc-CgYIARAAGAY....",
"scope": "https://www.googleapis.com/auth/drive",
"token_type": "Bearer",
"refresh_token_expires_in": 604799
}

登录CloudDrive

client_idclient_secretrefresh_token填入CloudDrive中,点击登录。

参考: https://www.333rd.net/zh/posts/tech/%E5%88%9B%E5%BB%BA%E8%B0%B7%E6%AD%8Coauth-client-id/