如何使用cURL连接到Google Drive API?

Wha*_*ion 7 curl google-drive-api google-oauth

假设有三个步骤,

  1. 获取设备代码,
  2. 获取身份验证令牌,
  3. 连接到Google云端硬盘.

第1步:如果(并且仅当)我忽略了redirect_url各种操作方法链接所必需的参数,我将完成步骤1 .所以这是...

curl -d 'client_id=*client_id*' -d 'scope=https://www.googleapis.com/auth/drive.file' -d 'response_type=code' 'https://accounts.google.com/o/oauth2/device/code'
Run Code Online (Sandbox Code Playgroud)

那时回归是...... {"device_code": "[device_code]", "user_code": "[user_code]", "expires_in": 1800, "interval": 5, "verification_url": "https://www.google.com/device"}

到现在为止还挺好.

第2步:这就是我被卡住的地方.尝试了以下各种迭代:

curl -H 'Content-Type: application/x-www-form-urlencoded' -d 'client_id=**client_id**' -d 'client_secret=*client_secret*' -d 'grant_type=authorization_code' -d 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' -d 'code=[device_code_from_above]' 'https://accounts.google.com/o/oauth2/token'
Run Code Online (Sandbox Code Playgroud)

以上回报

{"error" : "invalid_grant", "error_description" : "Malformed auth code."}
Run Code Online (Sandbox Code Playgroud)

如果grant_type更改为'http://oauth.net/grant_type/device/1.0',则响应为

{"error" : "invalid_request", "error_description" : "Parameter not allowed for this message type: redirect_uri"}
Run Code Online (Sandbox Code Playgroud)

如果redirect_uri删除,则响应为

{"error" : "authorization_pending"}
Run Code Online (Sandbox Code Playgroud)

上面的cURL尝试拼凑在一起,引用了以下链接... https://developers.google.com/identity/protocols/OAuth2ForDevices

http://www.visualab.org/index.php/using-google-rest-api-for-analytics#comment-157284

用curl列出google驱动器文件

Google云端硬盘未列出文件夹中的文件

无法获取Google OAuth 2.0访问令牌

https://www.daimto.com/google-authentication-with-curl/

陷入困境!

**编辑**根据请求,此处的目标是:开发一种在文件上传时收到警报的方法,并建立一个可以根据各种查询有选择地和系统地下载的系统.

我们之所以没有使用Google云端硬盘的网页用户界面:文件大小非常大:每个文件大小为10-50gb,谷歌无法先批量下载,也不能压缩任何小于我们最小的档案.

我们之所以没有使用Google云端硬盘的APP这样做:不可能(AFAIK)管理哪些文件本地下载和不下载,并且没有能力(再次AFAIK)存储到外部卷.

此外,我们正在将工作流数据库集成到我们的媒体上传和下载中:跟踪,日期,进度记录,版本等,这些都不是任何现有Google系统的一部分.因此,我们的目标是了解Google的API可能适用于所有这些选项.

DaI*_*mTo 5

第一步获取代码

https://accounts.google.com/o/oauth2/auth?client_id=[Application Client Id]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=[Scopes]&response_type=code
Run Code Online (Sandbox Code Playgroud)

将其放入浏览器窗口。并将代码复制到第二步。我怀疑问题出在您从第一步返回的代码中

第二步交换代码

您链接的我的博客文章中使用的代码有一个要点链接。

如您所见,帖子数据应作为一个长查询字符串发送,并用&分隔

--data'client_id = [应用程序客户端ID]&client_secret = [应用程序客户端密码]&refresh_token = [第二步授予的刷新令牌]&grant_type = refresh_token'\

googleauthenticationcurl.sh抓取的代码

# Client id from Google Developer console
# Client Secret from Google Developer console
# Scope this is a space seprated list of the scopes of access you are requesting.

# Authorization link.  Place this in a browser and copy the code that is returned after you accept the scopes.
https://accounts.google.com/o/oauth2/auth?client_id=[Application Client Id]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=[Scopes]&response_type=code

# Exchange Authorization code for an access token and a refresh token.

curl \
--request POST \
--data "code=[Authentcation code from authorization link]&client_id=[Application Client Id]&client_secret=[Application Client Secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
https://accounts.google.com/o/oauth2/token

# Exchange a refresh token for a new access token.
curl \
--request POST \
--data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&grant_type=refresh_token' \
https://accounts.google.com/o/oauth2/token
Run Code Online (Sandbox Code Playgroud)

-d不带(')

这似乎工作正常。我删除了不需要的标题以及代码中所有的('),我没有遇到任何问题,无法返回刷新令牌

curl -d client_id=103456123799103-vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com -d client_secret=uxpj6hx1H2N5BFqdnaNhIbie -d grant_type=authorization_code -d redirect_uri=urn:ietf:wg:oauth:2.0:oob -d code=4/AABvK4EPc__nckJBK9UGFIhhls_69SBAyidj8J_o3Zz5-VJN6nz54ew https://accounts.google.com/o/oauth2/token
Run Code Online (Sandbox Code Playgroud)

响应:

{
  "access_token" : "pO4LBSreV_r2i8kPklXVTqylXbMXip4OmQ0ZgRW0qZ8_b1ZP_zPJv0Xc_Qqsj9nM5ryWb7C81dYNFkO_bC6ifWA68dIlz40a0owG4GWpbZ2ufkHNXgre4",
  "expires_in" : 3600,
  "refresh_token" : "1/mEADfx6ffWULNBNFrKnlqOlK1uGL8Z546qBCHg",
  "token_type" : "Bearer"
}
Run Code Online (Sandbox Code Playgroud)