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

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

问题描述

假定有三个步骤,


  1. 获取设备代码,

  2. 获取身份验证令牌,

  3. 连接到Google云端硬盘。

步骤1:
如果(且仅当)我忽略了各种方法链接上必要的 redirect_url 参数。因此...

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,间隔:5, verification_url: https://www.google.com/device

到目前为止一切都很好。

So far so good.

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

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

如果 redirect_uri 被删除,响应为

{"error" : "authorization_pending"}

以上cURL尝试通过引用以下链接而拼凑在一起...
< a href = https://developers.google.com/identity/protocols/OAuth2ForDevices rel = nofollow noreferrer> 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云端硬盘未列出文件夹中的文件

无法获取Google OAuth 2.0访问令牌

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

记录!

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

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云端硬盘的网络用户界面,原因是:文件大小非常大:每个文件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云端硬盘的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.

推荐答案

第一步获取代码



step one get code

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.

您可以看到,帖子数据应作为一个长查询字符串发送,并用&

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


-data'client_id = [应用程序客户端ID]& 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不带(')



这似乎很好用。我删除了不需要的标题,并且删除了代码中所有的('),我没有遇到任何问题,无法返回刷新令牌

-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天全站免登陆