2015-11-19 62 views
1

我配置Devise使用omniauthFacebookGoogle。它與Facebook正常工作,但我面臨Google的問題。谷歌的OAuth 2.0,並制定 - 「錯誤」: 「redirect_uri_mismatch」

我一次又一次地得到這個錯誤:

ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, redirect_uri_mismatch:

{"error" : "redirect_uri_mismatch"}

[localhost] [127.0.0.1] [6a9377fe-d0b3-42] RuntimeError - Could not find a valid mapping for path "https://stackoverflow.com/users/auth/google_oauth2/callback":

devise (3.5.2) lib/devise/mapping.rb:49:in `find_by_path!'

我試了好幾種的URI在Google控制檯,但任何人似乎並沒有工作:

Google Console

的Gemfile

# Social Networks Authentification 
gem 'omniauth' 
gem 'omniauth-facebook' 
gem 'omniauth-google-oauth2' 

devise.rb

config.omniauth :facebook, ENV['OAUTH_FACEBOOK_ID'], ENV['OAUTH_FACEBOOK_SECRET'], 
         scope: 'public_profile', image_size: {height: 1600}, info_fields: 'name, id, first_name, last_name, gender, hometown, cover, email, link' # list of permissions 

# Not working, "error" : "redirect_uri_mismatch" 
config.omniauth :google_oauth2, ENV['OAUTH_GOOGLE_ID'], ENV['OAUTH_GOOGLE_SECRET'] 

omniauth_callbacks_controller.rb

def self.provides_callback_for(provider) 
    class_eval %Q{ 
     def #{provider} 
     @user = User.find_for_oauth(env["omniauth.auth"], current_user) 

     if @user.persisted? 
      handle_redirect("devise.#{provider}_uid", "#{provider}".capitalize) 
     else 
      session["devise.#{provider}_data"] = env["omniauth.auth"] 
      redirect_to new_user_registration_url 
     end 
     end 
    } 
    end 

    [:facebook, :google_oauth2].each do |provider| 
    provides_callback_for provider 
    end 

    def handle_redirect(_session_variable, kind) 
    # here we force the locale to the session locale so it siwtches to the correct locale 
    I18n.locale = session[:omniauth_login_locale] || I18n.default_locale 
    sign_in_and_redirect @user, event: :authentication 
    set_flash_message(:notice, :success, kind: kind) if is_navigational_format? 
    end 

omniauth_controller.rb

class OmniauthController < ApplicationController 
    def localized 
    # Just save the current locale in the session and redirect to the unscoped path as before 
    session[:omniauth_login_locale] = I18n.locale 
    redirect_to user_omniauth_authorize_path(params[:provider]) 
    end 
end 

的routes.rb

devise_for :users, skip: [:session, :password, :registration, :confirmation], controllers: { omniauth_callbacks: 'omniauth_callbacks' } 


    localized do 

    get 'auth/:provider' => 'omniauth#localized', as: :localized_omniauth 

    devise_for :users, :path => 'accounts', skip: :omniauth_callbacks, :controllers => {sessions: 'sessions', registrations: 'registrations', passwords: 'passwords'} 

end 

user.rb

devise :omniauthable, :omniauth_providers => [:facebook, :google_oauth2] 

我的問題都在本地生產

任何人都可以幫助我嗎?我真的不知道我還能做什麼。

+0

你能嘗試使用'http://127.0.0.1:3000/ [路徑]'和運行與該地址 –

+0

您的應用程序仍然沒有工作... :-( 但是這一次我有這樣的: 的https: //www.dropbox.com/s/3wok8pb7oob03ou/Capture%20d%E2%80%99%C3%A9cran%202015-11-19%20%C3%A0%2016.39.48.png?dl=0 –

+0

第一個時間錯誤是在我的控制檯上直接 –

回答

4

還好吧,終於發現出了什麼問題?

'omniauth-google-oauth2'寶石只有在'omniauth-oauth2'寶石的版本1.3.1的作品。

所以我改變了我的Gemfile這樣的:

# Social Networks Authentification 
gem 'omniauth-oauth2', '~> 1.3.1' # Don't touch that unless you don't want Google omniauth to work! 
gem 'omniauth-twitter' 
gem 'omniauth-facebook' 
gem 'omniauth-linkedin' 
gem 'omniauth-google-oauth2' # Works only with omniauth-oauth2 v 1.3.1 

而現在一切正常!

請參閱this thread瞭解更多詳情。

1

請檢查以下步驟來解決問題

Step1。錯誤:redirect_uri_mismatch出現以下錯誤,請複製以下鏈接文本請求中的重定向URI http://yourlinks:3000/與OAuth客戶端授權的重定向URI不匹配。訪問...

Step2。登錄到Google Console API,轉到Credential,選擇您的Api並通過上述鏈接重定向授權的授權部分

Step3。保存並再次測試

謝謝。

相關問題