“omniauth-twitter” email id is not fetched from twitter in ruby on rails

Multi tool use
Multi tool use


“omniauth-twitter” email id is not fetched from twitter in ruby on rails



i am using omniauth-twitter gem to enable Twitter login in my rails application. Here is my code ...



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 model -


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 code -


<%= link_to image_tag("login-twitter.png", alt: "twitter"), "/auth/twitter",:class => "popup", :"data-width" => "600", :"data-height" => "400" %>



Email id is not fetched from twitter. Please help





I used pry to debug a similar problem. Include 'pry' gem, and then add binding.pry just before where you expect the email Id to be fetched from twitter. You can then inspect the response from twitter and figure out what's happening. yorickpeterse.com/articles/debugging-with-pry should help you get started on pry.
– Prakash Murthy
May 7 '13 at 9:30


binding.pry





thanks for your comment, 'pry' awesome gem for debugging. Now it shows like this [1] pry(#<User>)> self.email = auth['extra']['raw_info']['email'] => nil [2] pry(#<User>)> self.name = auth['extra']['raw_info']['name'] => "Ginfy"
– Free-Minded
May 7 '13 at 9:49



[1] pry(#<User>)> self.email = auth['extra']['raw_info']['email'] => nil


[2] pry(#<User>)> self.name = auth['extra']['raw_info']['name'] => "Ginfy"





check this
– Free-Minded
May 7 '13 at 9:57






I would suggest you to read mail-archive.com/twitter-development-talk@googlegroups.com/…
– Deepak Lamichhane
Jul 4 '13 at 15:17




4 Answers
4



Twitter doesn´t give you the email via API.



This works if you are using omniauth-facebook gem for example, but twitter doesn´t offer you the email - you have to create a workaround.



For example ask the user in a second step to fill in his/her email address.





I've got around this by autopopulating email as "#{twitter_nickname}@example.org" and having the user change it at their leisure.
– Tom O'Connor
May 7 '13 at 10:44





Hey Tom, i'm currently having the Same Issue. Could you share your code on how to autopopulate the email ?
– The Mini John
Aug 31 '13 at 15:37





I would add a before_create hook and set the email their.
– Mattherick
Aug 31 '13 at 18:45





I'm currently at this Step, could you give it a look ? -> stackoverflow.com/questions/18504308/…
– The Mini John
Sep 1 '13 at 2:58






This may contain security holes. For example, I can auth in with my Twitter account and fill in somebody's email in the second step to gain access to their account. To counter this, one way could be adding a third step which requires email verification from the email.
– dvdchr
Dec 9 '14 at 9:10



The accepted answer is outdated. Twitter is providing email now via this Special Permission Form. Complete the form requesting special permissions and wait for approval.



Also you can see this answer for more info.



Omniauth gem fetch the users email



see: include_email=true



you have to enable the additional information
Additional Permission



The GEM is working fine the problem is Twitter do not return email for some reason. unlike facebook..


GEM


Twitter


return email


facebook





If you update your Twitter permissions and you are still not receiving the email field in the OmniAuth hash, you have to update your Twitter credential tokens in order to get the extra parameters.
– ZombieBsAs
Oct 12 '16 at 17:13






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

PdBtvT
L90Bx9hkDDfVxST83F6X9VArH0iBwL1A

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications