我想使用以下方式将 pdf 文件上传到 Google Drivecurl
,用于自动化测试目的。
我已经在 Google Cloud Platform 上创建了一个帐户(获取了客户端 ID 和 Secret)并启用了 Google Drive API。
使用 OAuth2 进行连接的所有方法都涉及 Web 浏览器或单击某些按钮,但我不打算这样做。
有没有什么方法可以完成使用 OAuth2 进行身份验证并获取访问令牌以便能够使用它上传文件的整个过程,使用ONLY curl
cmd终端中的命令?
Thanks.
以下命令将向您展示如何使用 Curl 向 Google 授权。您必须至少使用一次网络浏览器才能获取刷新令牌,一旦您拥有刷新令牌,您就可以使用该命令再次请求新的令牌。
# Client id from Google Developer console
# Client Secret from Google Developer console
# Scope this is a space separated 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=[Authentication 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
Tips:
上传分为两部分,您需要上传元数据,然后上传实际文件的文件流。
创建和更新之间存在差异。如果它是现有文件,您将需要使用更新而不是创建,否则您每次都会获得一个新文件。
代码摘自GoogleAuthenticationCurl.sh
服务帐户
如果你想完全解放双手,那么你应该查看一个服务帐户,我无法帮助你在curl中做到这一点,我什至不确定它是否可能,因为所有的安全性和令牌的生成。
刷新令牌过期
官方文档为刷新令牌以及它们可能的到期时间
- 用户已撤销您的应用的访问权限。
- 刷新令牌已六个月未使用。
- 用户更改了密码,并且刷新令牌包含 Gmail 范围。
- 用户帐户已超出授予的(实时)刷新令牌的最大数量。 (目前,每个 OAuth 2.0 客户端 ID 每个 Google 帐户最多可使用 50 个刷新令牌。如果达到该限制,创建新的刷新令牌会自动使最旧的刷新令牌失效,而不会发出警告。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)