2014-12-02 9 views
5

Ho seguito le istruzioni esattamente come nel http://sourcey.com/rails-4-omniauth-using-devise-with-twitter-facebook-and-linkedin/#changesDevia Omniauth + errore di collegamento. "Non è stato trovato. L'autenticazione passante"

Per implementare Devise Omniauth Linkedin nella mia app, ho fatto quanto segue,

In devise.rb

config.omniauth :linked_in, "*******", "**********" 

e nel mio modello di utente che è user.rb, ho avuto questa

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :lockable, :confirmable, :timeoutable and :omniauthable 
devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable 

TEMP_EMAIL_PREFIX = '[email protected]' 
TEMP_EMAIL_REGEX = /\[email protected]/ 
validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update 

def self.find_for_oauth(auth, signed_in_resource = nil) 

# Get the identity and user if they exist 
identity = Identity.find_for_oauth(auth) 

# If a signed_in_resource is provided it always overrides the existing user 
# to prevent the identity being locked with accidentally created accounts. 
# Note that this may leave zombie accounts (with no associated identity) which 
# can be cleaned up at a later date. 
user = signed_in_resource ? signed_in_resource : identity.user 

# Create the user if needed 
if user.nil? 

    # Get the existing user by email if the provider gives us a verified email. 
    # If no verified email was provided we assign a temporary email and ask the 
    # user to verify it on the next step via UsersController.finish_signup 
    email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email) 
    email = auth.info.email if email_is_verified 
    user = User.where(:email => email).first if email 

    # Create the user if it's a new registration 
    if user.nil? 
    user = User.new(
     name: auth.extra.raw_info.name, 
     #username: auth.info.nickname || auth.uid, 
     email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com", 
     password: Devise.friendly_token[0,20] 
    ) 
    user.skip_confirmation! 
    user.save! 
    end 
end 

# Associate the identity with the user if needed 
if identity.user != user 
    identity.user = user 
    identity.save! 
end 
user 
end 

def email_verified? 
self.email && self.email !~ TEMP_EMAIL_REGEX 
end 

end 

mio routes.db file di assomiglia a questo

devise_for :users, :controllers => { omniauth_callbacks: 'omniauth_callbacks' } 
match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup 

controllori/omniauth_callbacks_controller.rb

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

    if @user.persisted? 
     sign_in_and_redirect @user, event: :authentication 
     set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? 
    else 
     session["devise.#{provider}_data"] = env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 
} 
end 

[:linked_in].each do |provider| 
provides_callback_for provider 
end 

def after_sign_in_path_for(resource) 
if resource.email_verified? 
    super resource 
else 
    finish_signup_path(resource) 
end 
end 
def linkedin 
# You need to implement the method below in your model (e.g. app/models/user.rb) 
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) 

if @user.persisted? 
    sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
    set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
else 
    session["devise.facebook_data"] = request.env["omniauth.auth"] 
    redirect_to new_user_registration_url 
end 
end 
def action_missing(provider) 
# Set up authentication/authorizations here, and distribute tasks 
# that are provider specific to other methods, leaving only tasks 
# that work across all providers in this method. 
end 
end 

Quando clicco sul link per accedere con Linkedin appare il seguente errore:

Non trovato. Passthru di autenticazione.

Please help me.I non riesco a capire cosa è sbagliato, provato più volte ma lo stesso errore appare, grazie!

+1

correggi i rientri nel codice e cerca di evidenziare il problema (nel codice), quindi possiamo aiutarti. –

+0

Sono bloccato anche qui. Ho seguito lo stesso tuto per Facebook per un'altra app e ha funzionato come un fascino, ora lo sto facendo per LinkedIn e ho lo stesso problema. Parlerò con la mia app precedente una volta tornato a casa e cercherò di ottenere ciò che stiamo facendo male. Saluti. – juliangonzalez

+0

il metodo devise manca omniauth_providers: [: linked_in] argomento – Flov

risposta

13

Questo problema è dovuto al l'url che richiede http://localhost:3000/users/auth/linked_in/ invece di http://localhost:3000/users/auth/linkedin/

Per risolvere questo problema è necessario modificare nel vostro app/config/initializers/devise.rb questo:

config.omniauth :linked_in, LINKEDIN_CONFIG['APP_KEY'], LINKEDIN_CONFIG['APP_SECRET'] 

da questo:

config.omniauth :linkedin, LINKEDIN_CONFIG['APP_KEY'], LINKEDIN_CONFIG['APP_SECRET'] 
+1

grazie mille! Questo mi avrebbe richiesto anni per capire :) – shicholas

+0

il diavolo è nei dettagli :-) – Matthias

+0

Ran in un problema simile con un diverso plugin omniauth. Questo mi ha aiutato a risolvere rapidamente il problema. Grazie! –