gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:52:36 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com