如何使用 cURL 连接到 Google Drive API? [英] How to connect to the Google Drive API using cURL?

查看:69
本文介绍了如何使用 cURL 连接到 Google Drive API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有三个步骤,

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

第 1 步:当(且仅当)我忽略在各种操作方法链接中列出的必要 redirect_url 参数时,我才能完成第 1 步.所以这是...

STEP 1: I get through Step 1 if (and only if) I ignore the redirect_url parameter listed necessary on various how-to links. So it's...

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 步:这就是我卡住的地方.尝试了以下各种迭代:

STEP 2: This is where I get stuck. Tried various iterations of the following:

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',则响应为

If grant_type is changed to 'http://oauth.net/grant_type/device/1.0', response is

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

如果 redirect_uri 被删除,响应是

If redirect_uri is removed the response is

{"error" : "authorization_pending"}

上述 cURL 尝试是通过参考以下链接拼凑而成的...https://developers.google.com/identity/protocols/OAuth2ForDevices

The above cURL attempts were cobbled together referencing the following links... https://developers.google.com/identity/protocols/OAuth2ForDevices

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

使用 curl 列出谷歌驱动器文件

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

无法获取 Google OAuth 2.0 访问令牌

https://www.daimto.com/google-authentication-with-卷曲/

受阻!

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

Per request, the goal here: develop a method of being alerted when files upload, and have a system in place that can selectively and systematically download based on a variety of queries.

我们不使用 Google Drive 的 Web UI 执行此操作的原因:文件大小非常大:每个文件 10-50gb,并且 Google 无法在不先压缩的情况下进行批量下载,并且无法压缩任何超过大小的内容这比我们最小的文件还小.

The reason we're not doing this with Google Drive's web UI: The file sizes are pretty big: 10-50gb per file, and Google can't batch download without zipping first, and can't zip anything over a size that's smaller than our smallest file.

我们不使用 Google Drive 的 APP 执行此操作的原因:无法 (AFAIK) 管理哪些文件在本地下载和不下载,并且无法(再次 AFAIK)存储到外部卷.

The reason we're not doing this with Google Drive's APP: It's not possible (AFAIK) to manage which files do and don't download locally, and there's no ability (again AFAIK) to store to an external volume.

此外,我们正在将工作流数据库集成到我们的媒体上传和下载中:跟踪、日期、进度说明、版本等,这些都不是任何现有 Google 系统的一部分.所以这里的目标是看看 Google 的 API 可以为这一切提供哪些选择.

Also, we're integrating a workflow database into our media uploads and downloads: tracking, dates, progress notes, versions, etc., none of which is part of any existing Google system. So the goal here is to see what's options Google's API might hold for all this.

推荐答案

第一步获取代码

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

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

Put this into a browser window. And copy the code there into step two. I suspect the issue is with the code you are getting returned from the first step

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

There was a link to the gist for the code used in my blog post you linked.

正如你所看到的,post 数据应该作为一个用 & 分隔的长查询字符串发送

As you can see the post data should be sent as one long query string separated with &

--data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[第二步授予的刷新令牌]&grant_type=refresh_token'

--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 不带 (')'s

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

-d with out (')'s

This appears to work fine. I removed the header which isnt needed and all the (')'s you had in your code I didnt have any issues getting a refresh token returned

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"
}

这篇关于如何使用 cURL 连接到 Google Drive API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆