我可以使用devise :omniauthable
我可以使用此方法获取Facebook电子邮件并创建用户:
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token['extra']['user_hash']
if user = User.find_by_email(data["email"])
user
else # Create a user with a stub password.
User.create!(:email => data["email"], :password => Devise.friendly_token[0,20])
end
end
控制器:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def twitter
@user = User.find_for_twitter_oauth(env['omniauth.auth'])
if @user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', :kind => 'Twitter'
sign_in_and_redirect @user, :event => :authentication
else
flash[:notice] = I18n.t 'devise.omniauth_callbacks.failure', :kind => 'Twitter', :reason => 'User not found'
redirect_to new_user_session_path
end
end
end
如果我在传入的参数中使用类似这样的内容,我会得到user_id
,uid
,credentials
,secret
等等...但是没有电子邮件。我可以用这种方式得到它吗?我不想为users表添加新字段或添加包含用户id,oauth prowiders和oauth id的新表。
对于推特的看门人策略,是否存在更为干净的方式?
答案 0 :(得分:2)
Twitter API 不会返回电子邮件,因此由您决定如何处理