我正在试验谷歌的 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/