程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了仅使用 curl 获取 google Oauth2 访问令牌大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决仅使用 curl 获取 google Oauth2 访问令牌?

开发过程中遇到仅使用 curl 获取 google Oauth2 访问令牌的问题如何解决?下面主要结合日常开发的经验,给出你关于仅使用 curl 获取 google Oauth2 访问令牌的解决方法建议,希望对你解决仅使用 curl 获取 google Oauth2 访问令牌有所启发或帮助;

我想使用 curl 将 pdf 文件上传到谷歌驱动器(用于自动化测试目的), 我已经在谷歌云平台上创建了一个帐户(获得了客户端 ID 和秘密),并启用了谷歌驱动器 API。

使用 oauth2 进行连接的所有方法都涉及 Web 浏览器或单击某些按钮,我不打算这样做。

有什么方法可以使用 oauth2 进行身份验证的整个过程,并在 cmd 终端中使用 ONLY curl 命令获取访问令牌以便能够使用它上传文件?

谢谢。

解决方法

以下命令将向您展示如何使用 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

提示:

上传分为两部分,您需要上传您的元数据,然后是实际文件的文件流。

创建和更新是有区别的。如果它是一个现有文件,您将需要使用更新而不是创建,否则您每次都会获得一个新文件。

从 GoogleAuthenticationCurl.sh 中提取的代码

服务帐号

如果你想完全解放双手,那么你应该查看一个服务帐户,我无法帮助你在 curl 中做到这一点,我什至不确定它是否可能,因为所有的安全性和代币的生成。

刷新令牌过期

refresh tokens 的官方文档及其可能过期

  • 用户撤销了您的应用的访问权限。
  • 刷新令牌已六个月未使用。
  • 用户更改了密码,刷新令牌包含 Gmail 范围。
  • 用户帐户已超过授予的(实时)刷新令牌的最大数量。 (目前每个 OAuth 2.0 客户端 ID 每个 Google 帐户的刷新令牌限制为 50 个。如果达到限制,创建新的刷新令牌会自动使最旧的刷新令牌失效,而不会发出警告。)

大佬总结

以上是大佬教程为你收集整理的仅使用 curl 获取 google Oauth2 访问令牌全部内容,希望文章能够帮你解决仅使用 curl 获取 google Oauth2 访问令牌所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。