【问题标题】:redirect_uri_mismatch with OmniAuth Google oauth2 on Heroku在 Heroku 上与 OmniAuth Google oauth2 的 redirect_uri_mismatch
【发布时间】:2020-06-13 03:16:05
【问题描述】:

我正在尝试使用 OmniAuth 使用 google auth 设置一个简单的 Rails 应用程序。

在 heroku 上运行应用程序时,当我尝试直接或通过重定向访问 oauth 路由时出现以下错误:

redirect_uri_mismatch

请求详情:

access_type=offline
client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com
redirect_uri=http://stock-scraper-rails.herokuapp.com/auth/google_oauth2/callback
response_type=code
scope=email profile
state=94be59d4d241b70c83406ce59c36e7fc8d50279c

在本地工作得很好。我尝试使用 ngrok 隧道,它也可以。

完整网址:https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8

我的应用托管在http://stock-scraper-rails.herokuapp.com 你可以自己去 /auth/google_oauth2 查看错误。

我搜索了一下,但无法解决问题。以下是我已经尝试/做过但没有解决问题的方法:

  • 已将域添加到授权域
  • 类似问题的一些答案建议等待,因为有时 google 需要一段时间才能更新对域的更改。但是,我已经等了几个小时了,错误仍然存​​在
  • 双重/三重检查我的环境变量在 Heroku 上是否正确
  • 已检查 Heroku 日志;那里没有错误
  • 手动设置 OmniAuth.config.full_host

回调路由:

get '/auth/google_oauth2/callback', to: 'auth#oauth_callback'

顺便说一句,我没有使用设计。目前我只是想让控制器在会话中存储一些数据:

class AuthController < ApplicationController

  def oauth_callback
    authentication_google_data = request.env['omniauth.auth'].except(:extra)
    user_email = authentication_google_data['info']['email']
    # rest ommited
  end
end

OmniAuth 配置:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
end

相关宝石版本:

  • 导轨 (6.0.2.1)
  • omniauth (1.9.0)
  • omniauth-google-oauth2 (0.8.0)
  • omniauth-oauth2 (1.6.0)

还尝试将omniauth-oauth降级到1.3.1,因为读到有一个版本导致类似问题,但没有成功。

我可以尝试的任何其他想法都会非常有帮助:)

【问题讨论】:

    标签: ruby-on-rails ruby heroku omniauth google-oauth


    【解决方案1】:

    我发现了问题所在。在我的应用程序的谷歌开发者控制台上, OAuth 2.0 客户端 ID,我创建了一个类型为“其他”而不是“Web 应用程序”的 ID。

    https://console.cloud.google.com/apis/credentials?project=myproject 上创建一个类型为“Web 应用程序”的新地址,并将回调 url(http 和 https)添加到 授权重定向 URIs 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-29
      • 2014-06-12
      • 2021-06-22
      • 2014-07-01
      • 2014-04-26
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      相关资源
      最近更新 更多