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

问题描述 投票:0回答:2

我正在试验谷歌的REST apis。在这里,我试图从cURL命令生成访问令牌,以便在进一步的REST请求中使用它。但我面临以下错误。

我试图获取访问令牌tru下面的cURL命令,但我得到低于错误响应。

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

所以,请帮我解决错误。

android curl oauth-2.0 google-drive-sdk google-oauth
2个回答
1
投票

所以答案就在于问题。 "Invalid grant_type: client_credentials"而赠款类型应该是authorization_code

我给你的建议是去OAuth游乐场,比较它发送的内容和你发送的内容。这是来自操场的糊状物......

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

0
投票

不要使用urn:ietf:wg:oauth:2.0:oob

也不使用https://developers.google.com/oauthplayground

但是使用http://localhost ......

或任何web-server,它将post-back重写为本地主机名。

除非处理后回复,否则cURL请求本身是无用的。

而这个问题甚至没有透露这是在线还是在NAT之后。

对于服务器到服务器oAuth2流,您需要一个服务帐户的联机主机和凭据。

© www.soinside.com 2019 - 2024. All rights reserved.