Google OAuth 2授权 - 交换令牌代码

Dr *_*Kay 7 oauth-2.0 google-oauth

我正在尝试实施Google OAuth 2以访问Google API.我使用服务器端方案遵循指南.

获取代码没有问题,服务器重定向到localhost(这是目前重定向URI中唯一允许的服务器).要实现这一点,我去https://accounts.google.com/o/oauth2/auth?client_id=whatever.apps.googleusercontent.com&redirect_uri=http://localhost/&scope=https://www.google.com/m8/feeds/&response_type=code页面.

然后,我尝试使用curl(如在指南中)测试,如果谷歌的服务器响应访问令牌.然而,它似乎很难失败.只有我能得到的回应是{"error":"invalid_client"}.我确信我提供了Google希望我提供的所有内容 - 代码,客户端ID,客户端密钥,重定向URI(localhost)和grant_type=authorization_code.

整个curl命令行是:

curl https://accounts.google.com/o/oauth2/token -d "code=<code>&client_id=whatever.apps.googleusercontent.com&client_secret=<won't tell!>&redirect_uri=http://localhost&grant_type=authorization_code"
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?如何为访问令牌交换代码?

小智 3

你有urlencode你的客户端密码和重定向网址吗?这对我行得通。

应该http%3A%2F%2Flocalhost代替https://localhost.