在https://code.google.com/apis/console网站上,我已经注册了我的应用程序,设置生成的客户端ID:和客户端秘密到我的应用程序,并尝试登录谷歌。 不幸的是,我收到了错误信息:

Error: redirect_uri_mismatch
The redirect URI in the request: http://127.0.0.1:3000/auth/google_oauth2/callback did not match a registered redirect URI

scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
response_type=code
redirect_uri=http://127.0.0.1:3000/auth/google_oauth2/callback
access_type=offline
approval_prompt=force
client_id=generated_id

这条信息是什么意思,我该如何修复它? 我使用宝石omniauth-google-oauth2。


当前回答

这个问题的主要原因只会来自chrome和chrome处理WWW和非WWW不同取决于你如何在浏览器中输入你的URL,它从谷歌搜索并直接显示结果,所以发送的重定向URL在不同的情况下是不同的

添加所有可能的组合,你可以找到确切的url从fiddler, 400错误弹出不会给你确切的http和www信息

其他回答

重要补充:我发现在跨客户端服务器认证流程中,当你从Web SDK接收到serverAuthCode时,你应该使用“postmessage”,当你从Android或iOS SDK接收到serverAuthCode时,将redirect_uri设置为空。

对于我的web应用程序,我纠正了我的错误

instead of : http://localhost:11472/authorize/
type :      http://localhost/authorize/

在任何在客户端检索授权代码的流中,例如GoogleAuth.grantOfflineAccess() API,现在您希望将代码传递到服务器,赎回它,并存储访问和刷新令牌,那么您必须使用文字字符串postmessage而不是redirect_uri。

例如,基于Ruby文档中的代码片段:

client_secrets = Google::APIClient::ClientSecrets.load('client_secrets.json')
auth_client = client_secrets.to_authorization
auth_client.update!(
  :scope => 'profile https://www.googleapis.com/auth/drive.metadata.readonly',
  :redirect_uri => 'postmessage' # <---- HERE
)

# Inject user's auth_code here:
auth_client.code = "4/lRCuOXzLMIzqrG4XU9RmWw8k1n3jvUgsI790Hk1s3FI"
tokens = auth_client.fetch_access_token!
# { "access_token"=>..., "expires_in"=>3587, "id_token"=>..., "refresh_token"=>..., "token_type"=>"Bearer"}

唯一提到postmessage的谷歌文档是这个老谷歌+登录文档。下面是一个截图和存档链接,因为G+即将关闭,这个链接可能会消失:

离线访问的文档页没有提到这一点是绝对不可原谅的。# FacePalm指

当你在https://code.google.com/apis/console注册你的应用程序 创建一个客户端ID,你就有机会指定一个或多个重定向 uri。你的认证URI上的redirect_uri参数的值必须 完全匹配其中一个。

小心url末尾多余的/ http://localhost:8000和http://localhost:8000/不同