Oauth 2" redirect_uri_mismatch:{"错误" :" redirect_uri_mismatch" }"在铁轨

时间:2015-10-26 19:33:33

标签: ruby-on-rails ruby oauth

我正在使用oauth2 gem进行谷歌登录验证。我的代码看起来像

omniauth.rb

OmniAuth.config.logger = Rails.logger
require "omniauth-google-oauth2"
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, 'secret-client-id', 'secret-number', {client_options: {ssl: {ca_file: Rails.root.join("cacert.pem").to_s}}}
end

我的 routes.rb

  get 'auth/:provider/callback', to: 'sessions#create'
  get 'auth/failure', to: redirect('/')

我的视图

 <%= link_to "Sign in with Google", "/auth/google_oauth2", id: "sign_in" %>

我在这个页面之后得到这个URL enter image description here

enter image description here

我哪里错了?

1 个答案:

答案 0 :(得分:0)

尝试将令牌添加到omniauth初始化程序中:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, "id", "secret", {
    client_options: {ssl: {ca_file: Rails.root.join("cacert.pem").to_s}},
    scope: 'email profile',
    access_type: 'online',
    setup: (lambda do |env|
      request = Rack::Request.new(env)
      env['omniauth.strategy'].options['token_params'] = {:redirect_uri => 'http://.../auth/google_oauth2/callback'}
    end)
}
end