我正在使用omniauth-twitter gem在我的rails应用程序中启用Twitter登录。这是我的代码......
gemfile -
gem 'omniauth', '~> 1.1.1'
gem 'omniauth-twitter'
routes.rb -
match '/auth/twitter/callback', to: 'users#twitter_login'
match 'auth/failure', to: 'static_pages#home'
User_controller.rb -
def twitter_login
auth = request.env['omniauth.auth']
authentication = Authentication.find_by_provider_and_uid(auth['provider'],auth['uid'])
if authentication
sign_in authentication.user
redirect_to root_url
else
if(User.where(:email => auth['extra']['raw_info']['email']).exists?)
flash[:notice] = "You already have account in ibetter"
redirect_to root_url
else
user = User.new
user.apply_omniauth(auth)
if user.save(:validate => false)
sign_in user
flash[:notice] = "Welcome to Ginfy"
redirect_to root_url
else
flash[:error] = "Error while creating a user account. Please try again."
redirect_to root_url
end
end
end
end
session_helper.rb -
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
User.rb模型 -
before_save { |user| user.email = email.downcase }
def apply_omniauth(auth)
self.email = auth['extra']['raw_info']['email']
self.name = auth['extra']['raw_info']['name']
authentications.build(:provider => auth['provider'], :uid => auth['uid'], :token => auth['credentials']['token'])
end
erb代码 -
<%= link_to image_tag("login-twitter.png", alt: "twitter"), "/auth/twitter",:class => "popup", :"data-width" => "600", :"data-height" => "400" %>
电子邮件ID不是从twitter获取的。请帮忙
Twitter没有通过API向您发送电子邮件。
如果你使用omniauth-facebook gem,这可以工作,但是twitter没有为你提供电子邮件 - 你必须创建一个解决方法。
例如,在第二步中要求用户填写他/她的电子邮件地址。
接受的答案已过时。 Twitter现在通过这个Special Permission Form提供电子邮件。填写请求特殊权限的表单并等待批准。
您还可以查看this answer获取更多信息。
GEM
工作正常问题是Twitter
由于某种原因没有return email
。不像facebook
..