简体   繁体   English

如何从omniauth获取有关Rails 4的Facebook用户电子邮件?

[英]How to get the Facebook user email from omniauth for Rails 4?

How can i get Facebook user email by omniauth for Rails? 我如何通过omniauth for Rails获取Facebook用户电子邮件? This is my omniauth.rb 这是我的omniauth.rb

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_KEY'] , ENV['FACEBOOK_SECRET'], :scope => 'email', :display => 'popup', :info_fields => 'name,email'
end

This is my model 这是我的模特

def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
      puts "auth.providerauth.providerauth.providerauth.provider"
      puts auth.info.inspect

      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.username = auth.info.name.gsub(" ","") + new_token
      user.email = auth.info.email
      user.password = digest(new_token)
      user.save!
    end
  end

But i only got this from the info 但是我只是从信息中得到的

auth.providerauth.providerauth.providerauth.provider
#<OmniAuth::AuthHash::InfoHash image="http://graph.facebook.com/xxxx" name="xxx xxxx">

Seems like no email in return. 好像没有电子邮件作为回报。 But i need the email to pass my model validation so what should i do? 但是我需要电子邮件通过我的模型验证,所以我该怎么办?

Thanks! 谢谢!

in theory the user.auth.info.email should be the case 理论上, user.auth.info.email应该是这种情况

https://github.com/mkdynamic/omniauth-facebook https://github.com/mkdynamic/omniauth-facebook

... but in reality Facebook don't necessary need to return email if user don't want to publish email. ...但是实际上,如果用户不想发布电子邮件,Facebook不需要返回电子邮件。 (for example Twitter will never give you response with email) (例如,Twitter永远不会通过电子邮件给您回复)

so the approach where you creating user directly from what Oauth callback returns may be wrong for you. 因此,直接从Oauth回调返回的结果中创建用户的方法可能对您来说是错误的。

if you really rally need an email from user, try rather saving whatever the OAuth returns to an Identity model and then if email is present create User and if email is not present prompt the user to provide the email in "finish signup form" that will create the user. 如果您确实需要用户发送电子邮件,请尝试保存OAuth返回给Identity模型的所有内容,然后如果存在电子邮件,请创建用户;如果不存在电子邮件,则提示用户以“完成注册表格”的形式提供电子邮件创建用户。

Think about it: Technically speaking Oauth is a protocol where user can signup to your application without providing any credentials => the Provide-UID is what defines him not the email 考虑一下:从技术上讲,Oauth是一个协议,用户可以在不提供任何凭据的情况下注册您的应用程序=> Provide-UID is what defines him而不是电子邮件的人

So you either split the way how you handle session to User(email&password) or Identity(OAuth) so someting like current_user || current_identity 因此,您可以拆分处理与User(email&password) or Identity(OAuth)会话的方式,这样就可以像current_user || current_identity current_user || current_identity in your controllers ....or you will link them when email is provided 控制器中的current_user || current_identity ....或在提供电子邮件时将其链接

它存在于身份验证中。

auth.info.email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM