使用facebook身份验证rails访问电子邮件

时间:2013-05-20 13:43:36

标签: ruby-on-rails

我无法通过facebook身份验证访问用户电子邮件。 我在身份验证控制器中的代码

def facebook

     omni= request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omni['provider'],omni['uid'])
    if authentication
        flash[:notice]="Logged in successfully"
        sign_in_and_redirect User.find(authentication.user_id)
    elsif current_user
        token=omni['credentials'].token
        token_secret=omni['credentials'].secret

        current_user.authentications.create!(:provider=>omni['provider'],:uid=>omni['uid'],:token=>token,:token_secret=>token_secret)
        flash[:notice]="Authentication successful."
        sign_in_and_redirect current_user
    else
        user=User.new
    #   user.email=omni['extra']['raw_info'].email
        user.apply_omniauth(omni)
        if user.save
            flash[:notice]="Logged in."
            sign_in_and_redirect User.find(user.id)
        else
            session[:omniauth]=omni.except('extra')
            redirect_to new_user_registration_path
        end 
    end
end

devise.rb

config.omniauth:facebook,'xxxxxxxxxxxxxxx851','xxxxxxxxxxxxxxxxxxd47aae3',{:scope => 'email,publish_stream,offline_access',:display => 'popup'}

用户模型中的

def apply_omniauth(omni)
  authentications.build(:provider => omni['provider'],
  :uid=>omni['uid'],
  :token=>omni['credentials'].token,
  :token_secret=>omni['credentials'].secret,
  )

  x=omni["info"]["name"].split                              # split the full name into first and last name                                  
  self.first_name=x[0]
  self.last_name=x[1]
  self.profile_name=omni['info']['nickname']                  

他们的代码是什么?

1 个答案:

答案 0 :(得分:0)

最后,我记得email可以找到omni.info.email 另外,我建议你在这里使用.表示法,而不是哈希表。