加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 00:59:02 所属栏目:Linux 来源:网络整理
导读:假设有三个步骤, 获取设备代码, 获取身份验证令牌, 连接到Google云端硬盘. 步骤1: 如果(并且仅当)我忽略了在各种how-to链接上列出的redirect_url参数,我将通过步骤1.所以这是… curl -d 'client_id=*client_id*' -d 'scope=https://www.googleapis.com/auth
假设有三个步骤,

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

步骤1:
如果(并且仅当)我忽略了在各种how-to链接上列出的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'

那时回归是……
????{“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'

以上回报

{"error" : "invalid_grant","error_description" : "Malformed auth code."}

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

{"error" : "invalid_request","error_description" : "Parameter not allowed for this message type: redirect_uri"}

如果删除了redirect_uri,则响应为

{"error" : "authorization_pending"}

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

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

list google drive files with curl

Google Drive not listing files in folder

Not able to fetch Google OAuth 2.0 access token

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

陷入困境!

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

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

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

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

解决方法

第一步获取代码

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

将其放入浏览器窗口.并将代码复制到第二步.我怀疑问题是你从第一步返回的代码

第二步交换代码

在我链接的博客文章中使用的代码有一个链接.

正如您所看到的那样,帖子数据应该作为一个用&分隔的长查询字符串发送.

–data ‘client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&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

-d with out(‘)

这似乎工作正常.我删除了不需要的标头和你的代码中的所有(‘)你没有任何问题获得刷新令牌返回

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

响应:

{
  "access_token" : "pO4LBSreV_r2i8kPklXVTqylXbMXip4OmQ0ZgRW0qZ8_b1ZP_zPJv0Xc_Qqsj9nM5ryWb7C81dYNFkO_bC6ifWA68dIlz40a0owG4GWpbZ2ufkHNXgre4","expires_in" : 3600,"refresh_token" : "1/mEADfx6ffWULNBNFrKnlqOlK1uGL8Z546qBCHg","token_type" : "Bearer"
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读