我完全按照http://sourcey.com/rails-4-omniauth-using-devise-with-twitter-facebook-and-linkedin/#changes
中的说明进行操作要在我的应用程序中实现Devise Omniauth Linkedin,我做了以下工作,
在devise.rb
config.omniauth :linked_in, "*******", "**********"
在我的用户模型 user.rb 中,我有了这个
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 = 'change@me'
TEMP_EMAIL_REGEX = /\Achange@me/
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
我的 routes.db 文件如下所示
devise_for :users, :controllers => { omniauth_callbacks: 'omniauth_callbacks' }
match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup
控制器/ 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
当我点击链接以使用Linkedin登录时,会出现以下错误:
未找到。身份验证passthru。
请帮助我。我不会轻易搞错,一次又一次地尝试但是出现同样的错误,谢谢!
答案 0 :(得分:13)
此问题是由于请求http://localhost:3000/users/auth/linked_in/
而不是http://localhost:3000/users/auth/linkedin/
的网址
要解决此问题,您需要更改app/config/initializers/devise.rb
这个:
config.omniauth :linked_in, LINKEDIN_CONFIG['APP_KEY'], LINKEDIN_CONFIG['APP_SECRET']
由此:
config.omniauth :linkedin, LINKEDIN_CONFIG['APP_KEY'], LINKEDIN_CONFIG['APP_SECRET']