android - 如何使用 curl 命令获取 google oAuth2.0 访问 token ?

标签 android curl oauth-2.0 google-drive-api google-oauth

我正在试验谷歌的 REST api。在这里,我试图从 cURL 命令生成访问 token ,以便在进一步的 REST 请求中使用它。 但是我遇到了以下错误。

我试图通过以下 cURL 命令获取访问 token ,但我收到以下错误响应。

curl \
--request POST \
--data "code=4/GQEg70zaxHAuRhhd6A1RB_6LIxwwBV8ak5xRP-nZIBTjuvt4g3fTWyU&client_id=954040553015-bphgid2596t65i91827omteq778cp7gj.apps.googleusercontent.com&client_secret=Sn3giYFFPMCNteKC--938xsP&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
https://accounts.google.com/o/oauth2/token

响应:

{
"error": "invalid_grant",
"error_description": "Bad Request"
}

然后我按如下方式编辑了命令,但它失败了。

curl -d client_id=954040553015-bphgid2596t65i91827omteq778cp7gj.apps.googleusercontent.com -d client_secret=Sn3giYFFPMCNteKC--938xsP -d grant_type=authorization_code -d redirect_uri=urn:ietf:wg:oauth:2.0:oob -d code=4/GQEg70zaxHAuRhhd6A1RB_6LIxwwBV8ak5xRP-nZIBTjuvt4g3fTWyU https://oauth2.googleapis.com/token

响应:

{
  "error": "invalid_grant",
  "error_description": "Bad Request"
}

因此,基于此链接 https://gist.github.com/LindaLawton/cff75182aac5fa42930a09f58b63a309#file-googleauthenticationcurl-sh我已经更改了 grant_type=client_credentials。但我再次收到错误响应。

{
"error": "unsupported_grant_type",
"error_description": "Invalid grant_type: client_credentials"
}

所以,请帮我解决这个错误。

最佳答案

所以答案就在问题中。 "Invalid grant_type: client_credentials" 而授权类型应该是 authorization_code

我给你的建议是去 OAuth Playground ,比较它发送的内容和你发送的内容。这是 Playground 上的粘贴...

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-length: 277
content-type: application/x-www-form-urlencoded
user-agent: google-oauth-playground
code=4%2FGgFtOcUM73dTMJNpE7XR7w082MrYH-LCm7zMylg31ESKrwmpyQXnzOM
 &redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground
 &client_id=407408718192.apps.googleusercontent.com
 &client_secret=************
 &scope=
 &grant_type=authorization_code

关于android - 如何使用 curl 命令获取 google oAuth2.0 访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55363098/

相关文章:

android - 如何在 React-Native 中在没有 Internet 的情况下通过 Wifi 发送和接收数据

javascript - 如何使用 NodeJS 计算 UPS 运费?

php - 使用 curl 命令 linux 调用 codeigniter URL

python - 如何使用 GoogleOAuth2Mixin 使用 tornado 3.2.2 进行谷歌登录授权

Azure 流量管理器和 OAuth 2.0

Android OAuth2 Bearer token 最佳实践

android - 连接 Android 设备后 Eclipse 调试 View 中的 NullPointerException

Android Spinner 下拉 View 不透明

java - 如何从 Activity C 恢复 Activity B?

php - 我的密码 curl 有时会失败?