Google OAuth 2.0 redirect_uri_mismatch 跨客户端单点登录错误

Google OAuth 2.0 redirect_uri_mismatch error for cross-client single-sign on

我们正在尝试实现 Google 的 OAuth 2.0 跨客户端登录功能,以便我们的服务器保留令牌并将它们与用户相关联,如此处的流程图所示:Google OAuth 2.0 Server-Side Flow

我能够在客户端应用程序上成功检索到一次性访问代码。然后我将该代码通过 post 发送到服务器到“http://example.com/oauth2callback/code=

它很好地到达了服务器。然后服务器尝试 POST 到 Google,看起来像这样:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code={My Code}&
client_id={My Client ID}&
client_secret={My Client Secret}&
redirect_uri="http://example.com/oauth2callback"&
grant_type=authorization_code

但是,每次服务器返回"Error: redirect_uri_mismatch."

我们什么都试过了。我们在 Google 控制台中仔细检查了 redirect_uri 完全匹配,并且客户端 ID 和客户端密码是正确的。它仍然不起作用。有什么想法吗?

我们最终解决了这个问题,但我想 post 在这里,以便其他人可以找到它。事实证明,如果您通过从您自己的服务器与 Google 的服务器通信来交换访问令牌的一次性访问代码,则不应指定重定向 URI。相反,它应该是这样的:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code={My Code}&
client_id={My Client ID}&
client_secret={My Client Secret}&
redirect_uri=''&
grant_type=authorization_code

在 "server-side" 流程中,您的 redirect_uri 应设置为 postmessage。不幸的是,Google 没有清楚地记录这一点。另见 和相关的 questions/answers.