ruby-on-rails Devise Omniauth + Linkedin错误,“没找到,身份验证直通”

ukqbszuj  于 2023-05-19  发布在  Ruby
关注(0)|答案(2)|浏览(163)

我完全按照www.example.com中的说明操作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

controllers/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时,出现以下错误:
未找到。身份验证通过。
请帮助我。我不明白什么是错的,尝试了一次又一次,但同样的错误出现,谢谢!

hgc7kmma

hgc7kmma1#

此问题是由于请求http://localhost:3000/users/auth/linked_in/而不是http://localhost:3000/users/auth/linkedin/的URL造成的
要解决这个问题,您需要在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']
ruyhziif

ruyhziif2#

我在从Rails 5.2升级到7.0时遇到了同样的问题。故障排除部分www.example.com中提到了该问题https://github.com/heartcombo/devise/wiki/OmniAuth:-Overview#request-phase-initiated-doesnt-trigger
Omniauth要求策略中间件在中间件列表中注册。通过bin/rails middleware查看列表
我在初始化器/device.rb中有一些可自动加载的类,所以我必须将devise配置 Package 在一个块Rails.application.config.to_prepare中。因此,中间件列表中不会显示任何策略。
移除 Package 器使其再次工作
希望它能在升级Rails时对其他人有所帮助

相关问题