redirect_uri_mismatch 在 Heroku 上使用 OmniAuth Google oauth2

redirect_uri_mismatch with OmniAuth Google oauth2 on Heroku

我正在尝试使用 google auth.

通过 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 隧道,它也有效。

完整 url: 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 自己查看错误。

我搜索了一下,没有解决问题。这是我已经tried/did,但没有解决问题的方法:

回调路由:

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

相关 gems 版本:

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

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

我明白问题出在哪里了。在我的应用的 google 开发者控制台上, OAuth 2.0 客户端 ID,我创建了一个类型为 "Other" 而不是 "Web application" 的 ID。

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