ruby-on-rails - 设计Omniauth + Linkedin错误。 “Not found. Authentication passthru”

标签 ruby-on-rails devise linkedin omniauth

我完全按照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

Controller /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时,出现以下错误:

未找到。身份验证直通。

请帮助我。我不会理解什么地方出了什么问题,一次又一次尝试,但是出现了同样的错误,谢谢!

最佳答案

此问题是由于请求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']

关于ruby-on-rails - 设计Omniauth + Linkedin错误。 “Not found. Authentication passthru”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27248166/

相关文章:

ruby-on-rails - 当 cache_classes = false 时,为什么包含在 Rails 引擎初始化程序中会发生故障?

ruby-on-rails - Heroku 暂存环境未部署所有图像和 CSS

ruby-on-rails - 使用 Devise 和 devise_ldap_authenticable 时如何捕获 Ldap 错误并重定向到指定页面

postgresql - 为什么设计注册#create,:trackable doing an INSERT instead of UPDATE?

javascript - 拒绝运行 JavaScript URL,因为它违反了以下内容安全策略指令

android - 下载官方安卓 LinkedIn APK

javascript - HTML如何将&lt;input type ="number">的值设置为无穷大

ruby-on-rails - 设计可重新确认不发送

api - LinkedIn API 获取公司的所有员工?

ruby-on-rails - Modelwhichbelongs_to可以是几个模型?