完成401 Unauthorized facebook登录并重定向到特定页面。

时间:2014-07-21 18:44:06

标签: ruby-on-rails facebook devise

我尝试使用omniauth-facebook

实施的facebook登录后重定向到一个页面

这是控制器中的facebook方法

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in(@user) 
      redirect_to page_path('chooseapplication')
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else 
      session["devise.facebook_data"] = request.env["omniauth.auth"]
    end
  end 
end 

,这是用户模型中的方法

  def self.find_for_facebook_oauth(auth)
    user = User.where(:provider => auth.provider, :uid => auth.uid).first
    if user 
      return user 
    else 
      registered_user = User.where(:email => auth.info.email).first 
      if registered_user 
        return registered_user
      else 
        user = User.create(name:auth.extra.raw_info.name, provider:auth.provider, uid:auth.uid, email:auth.info.email, password:Devise.friendly_token[0,20], avatar:auth.info.image)
      end
    end
  end 

我登录后继续获取/ users / sign_in。

Started GET "/users/auth/facebook/callback?code=AQCbhZNNabqwKP5O9mzef3VD6BJeslKpdm5L9VDZ3KBfooH4hIxaAivzoVVuGjVU8iIYwJ7ajm_mEd8QAT4r83Gl6YAh5MdigdX5_hgqLiILuIOJfzxNDuWYNOg-1p57hkSqSlniyHPvGAFczKiIKMn8Mo1NDf18-wdNXtViBvT6uCbrYUxR8nCX4p_NgPM5rH002YFHcUgnjNM9kNZNl54gPEE4JZthK811BRtR_945ak_vhyXNwiTjWy5aLvZANJ_VqDpvt3JGITX2UP8pdSI5zvXhS1z1kRvi-YDaxT4nwM4Z59HV1yanSKCl5cUhdps16dF7uj7UdpSf0_LDw5jX&state=59e6e31655b64768607a2864f41242c2aea1c1b34ec17ca1" for 127.0.0.1 at 2014-07-21 15:50:51 -0400
I, [2014-07-21T15:50:51.763095 #54284]  INFO -- omniauth: (facebook) Callback phase initiated.
Processing by OmniauthCallbacksController#facebook as HTML
  Parameters: {"code"=>"AQCbhZNNabqwKP5O9mzef3VD6BJeslKpdm5L9VDZ3KBfooH4hIxaAivzoVVuGjVU8iIYwJ7ajm_mEd8QAT4r83Gl6YAh5MdigdX5_hgqLiILuIOJfzxNDuWYNOg-1p57hkSqSlniyHPvGAFczKiIKMn8Mo1NDf18-wdNXtViBvT6uCbrYUxR8nCX4p_NgPM5rH002YFHcUgnjNM9kNZNl54gPEE4JZthK811BRtR_945ak_vhyXNwiTjWy5aLvZANJ_VqDpvt3JGITX2UP8pdSI5zvXhS1z1kRvi-YDaxT4nwM4Z59HV1yanSKCl5cUhdps16dF7uj7UdpSf0_LDw5jX", "state"=>"59e6e31655b64768607a2864f41242c2aea1c1b34ec17ca1"}
  User Load (0.5ms)  SELECT  `berkleeflo_users`.* FROM `berkleeflo_users`  WHERE `berkleeflo_users`.`provider` = 'facebook' AND `berkleeflo_users`.`uid` = '699863416762705'  ORDER BY `berkleeflo_users`.`id` ASC LIMIT 1
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
Completed 401 Unauthorized in 15ms


Started GET "/users/sign_in" for 127.0.0.1 at 2014-07-21 15:50:52 -0400

我在stackoverflow上的某个地方读到了未经授权的错误意味着我有一个参数签名问题。

这是我的应用程序控制器

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

#Catch all CanCan errors and alert the user of the exception
  rescue_from CanCan::AccessDenied do | exception |
    redirect_to root_url, alert: exception.message 
  end

protect_from_forgery

  def not_found(msg="Not Found")
    raise ActionController::RoutingError.new(msg)
  end

  def after_sign_in_path_for(resource_or_scope)
    page_path('chooseapplication')
  end 

  def after_sign_out_path_for(resource_or_scope)
    root_url
  end

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :high_school_graduation_year << :role << :uid << :provider
    devise_parameter_sanitizer.for(:sign_in) << :first_name << :last_name << :high_school_graduation_year << :role << :uid << :provider << :email << :password
    devise_parameter_sanitizer.for(:account_update) << :high_school_graduation_year << :country_of_residence << :primary_language << :password << :password_confirmation
  end
end

我刚刚添加了:sign_in,并确保包含:provider:uid,但我仍然收到未经授权的消息。

1 个答案:

答案 0 :(得分:0)

事实证明我没有确认帐户。这就是我未经授权访问的原因。