我正在做这个请求:
POST request to
https://accounts.google.com/o/oauth2/token?
client_secret=xxxxx-x8U-tDOJbCPN3&
grant_type=authorization_code&
redirect_uri=[url]
client_id=xxxxxx-e4u1224f7uqjafv7m1lu2ek5ac01isi4.apps.googleusercontent.com&
code=xxxxxxlpdiZfd_LhYu167gK.QvJZYyoaoQAQ3oEBd8DOtNC9kGrnjwI
因此,正如文档所说,我使用这些 header 发出该请求:
"Accept-Encoding": "gzip"
"Content-Type": "application/x-www-form-urlencoded"
但是当我这样做时,我得到了 400:
{"error":"invalid_request","error_description":"Required parameter is missing: grant_type"}
最佳答案
我已经解决了这个问题。问题是我在查询字符串中发送参数,正确的方法是将它们作为 x-www-form-urlencoded
发送。所以正确的请求必须是这样的:
POST /o/oauth2/token? HTTP/1.1
Host: accounts.google.com
Cache-Control: no-cache
Postman-Token: b38df5b3-b64f-338a-1374-220647ee05a0
Content-Type: application/x-www-form-urlencoded
client_secret=xxxxx-x8U-tDOJbCPN3%26&grant_type=authorization_code&redirect_uri=myredirecturi&client_id=xxxxxx-e4u1224f7uqjafv7m1lu2ek5ac01isi4.apps.googleusercontent.com&code=xxxxxxlpdiZfd_LhYu167gK.QvJZYyoaoQAQ3oEBd8DOtNC9kGrnjwI
希望对你有帮助
关于java - google oauth api v2 中缺少 grant_type 参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409069/