【问题标题】:email access using facebook authentication rails使用 facebook 身份验证 rails 访问电子邮件
【发布时间】:2013-05-20 21:54:49
【问题描述】:

我无法通过 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

设计.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']                  

结束

代码中有他们的东西吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

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

    【讨论】:

    • :当我提出 request.env["omniauth.auth"] 时,我没有找到电子邮件字段。
    • 但您在 scope 中请求了该权限。因此,如果用户同意,FB 必须返回。
    猜你喜欢
    • 2023-03-22
    • 2023-03-18
    • 2012-08-10
    • 1970-01-01
    • 2015-03-21
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多