【发布时间】: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