【问题标题】:Getting "Error: redirect_uri_mismatch" with google_oauth2使用 google_oauth2 获取“错误:redirect_uri_mismatch”
【发布时间】:2012-11-02 23:35:40
【问题描述】:

网址看起来正确(昨天最后一次更新):

文件也是:

omniauth.rb:

provider :google_oauth2, 'MY_CLIENT_ID.apps.googleusercontent.com', 'MY_CLIENT_SECRET',
           :scope => 'https://mail.google.com/mail/feed/atom/'

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

header.html.erb

<li><%= link_to "Sign in with Google", "auth/google_oauth2" %></li>

routes.rb:

match '/auth/:provider/callback', to: 'sessions#omniauth_create'

但我明白了:

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

(Twitter 和 Facebook OmniAuth 运行良好)

不确定是什么问题。有什么建议可以解决这个问题吗?

编辑

我把 URI 改成了http...:

但仍然出现同样的错误。

【问题讨论】:

标签: ruby-on-rails omniauth


【解决方案1】:

看起来请求正在访问http://localhost:3000/auth/google_oauth2/callback,但您指定的与类似模式匹配的重定向 URI 是针对https。将http://localhost:3000/auth/google_oauth2/callback 添加到您的重定向列表可能会解决该问题。

编辑:另一个可能的解决方法是在相应的重定向 URI 中包含一个尾随 /,这似乎在这种情况下有效。

【讨论】:

  • 谢谢。我也以为是。我添加了http://localhost:3000 重定向并更新了omniath.rb。但仍然出现同样的错误(请参阅我的 EDIT)。
  • @alexchenco 啊,我明白了。您是否尝试将httphttps 添加到URI 列表中?我同意看起来你做的一切都是正确的,但我通常默认在涉及 OAuth 时添加 httphttps URI。另外,长镜头,但是您是否尝试过将尾随 / 添加到 URI 中?所以可能有四个 URI(过分杀伤力,是的 :)):httphttps,每个都包括尾随的正斜杠,没有它。
  • 非常感谢!添加以添加尾随 /。很奇怪,我的 Rails 应用没有尾随 /,而且 Google 提供的示例也不包含它。
  • @alexchenco 太棒了!我什至不想尝试理论化原因,因为我会错的,但我不止一次遇到同样的问题,所以我默认将所有可能的组合扔在那里。在这些日子里,我会变得更有效率:)
  • 在重定向 uris 末尾添加 ?close 对我有帮助。
【解决方案2】:

omniauth-oauth2 gem 版本 1.4 https://github.com/intridea/omniauth-oauth2/issues/81#issuecomment-151038559 有一个相对新鲜的问题

临时修复是在 Gemfile 中显式降级该 gem

gem 'omniauth-oauth2', '~> 1.3.1'

【讨论】:

  • 他们说要避免“谢谢”,所以我会说“爱你”这样一个简单的解决方案:*
【解决方案3】:

vi config/initializers/omniauth.rb OmniAuth.config.full_host = 'https://localhost:3000' Rails.application.config.middleware.use OmniAuth::Builder do provider :google_oauth2, 'google_client_id', 'google_client_secret' end

【讨论】:

    【解决方案4】:

    分享一个对我有用的非常简单的复制粘贴解决方案。

    我将我在代码中指定的任何内容复制为redirectUri,即"redirect_uri": "http://127.0.0.1:3001/",并将此keyvalue 粘贴到Google 设置 中,要求授权重定向URI。这样可以确保两个参数相同。

    如果计算出 url,我会 console.log() 它并从控制台窗口复制它,然后将其粘贴到谷歌设置中。

    【讨论】:

      【解决方案5】:

      我尝试了以上所有方法,但对我不起作用。最后在我的错误消息中注意到我的回电略有不同。我在 localhost:3000 和 auth 之间有一个用户。不太清楚为什么。

      http://localhost:3000/users/auth/google_oauth2/callback
      

      改了,等了30分钟就成功了。

      【讨论】:

        猜你喜欢
        • 2019-11-10
        • 1970-01-01
        • 1970-01-01
        • 2020-07-29
        • 2014-11-16
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        • 1970-01-01
        相关资源
        最近更新 更多