繁体   English   中英

redirect_uri_mismatch 与 Heroku 上的 OmniAuth Google oauth2

[英]redirect_uri_mismatch with OmniAuth Google oauth2 on Heroku

我正在尝试使用谷歌身份验证通过 OmniAuth 设置一个简单的 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 : 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 access_type 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 offline 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 client_id 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 631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com 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 redirect_uri 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 3Astock%2F% 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 自己查看错误。

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

  • 将域添加到授权域
  • 类似问题的一些答案建议等待,因为有时谷歌需要一段时间才能更新域的更改。 但是,我已经等了几个小时,错误仍然存​​在
  • 双重/三重检查我的环境变量在 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)
  • omn​​iauth (1.9.0)
  • omn​​iauth-google-oauth2 (0.8.0)
  • omn​​iauth-oauth2 (1.6.0)

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

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

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

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM