13

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

Free-Minded
  • 5,322
  • 6
  • 50
  • 93
  • 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. http://yorickpeterse.com/articles/debugging-with-pry/ should help you get started on pry. – Prakash Murthy May 07 '13 at 09:30
  • thanks for your comment, 'pry' awesome gem for debugging. Now it shows like this `[1] pry(#)> self.email = auth['extra']['raw_info']['email'] => nil` `[2] pry(#)> self.name = auth['extra']['raw_info']['name'] => "Ginfy"` – Free-Minded May 07 '13 at 09:49
  • [check this](http://stackoverflow.com/questions/3599621/is-there-a-way-to-get-an-users-email-id-after-verifying-her-twitter-identity-us) – Free-Minded May 07 '13 at 09:57
  • I would suggest you to read http://www.mail-archive.com/twitter-development-talk@googlegroups.com/msg12224.html – Deepak Lamichhane Jul 04 '13 at 15:17

5 Answers5

14

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.

Matthias
  • 4,355
  • 2
  • 25
  • 34
  • 5
    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 07 '13 at 10:44
  • Hey Tom, i'm currently having the Same Issue. Could you share your code on how to autopopulate the email ? – Mini John Aug 31 '13 at 15:37
  • I would add a before_create hook and set the email their. – Matthias Aug 31 '13 at 18:45
  • I'm currently at this Step, could you give it a look ? -> http://stackoverflow.com/questions/18504308/getting-omniauth-facebook-and-omniauth-twitter-work – Mini John Sep 01 '13 at 02: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 09 '14 at 09:10
  • @dvdchr you are absolutely right, a new user must confirm his email first. But this was not the question ;-). There was an other question about that in the past..take a look at my answer: http://stackoverflow.com/questions/18504308/getting-omniauth-facebook-and-omniauth-twitter-work/18557942#18557942 - this also contains the controller logic for the email confirmation.. – Matthias Dec 09 '14 at 13:38
5

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.

Community
  • 1
  • 1
ZombieBsAs
  • 171
  • 3
  • 14
4

Omniauth gem fetch the users email

see: include_email=true

you have to enable the additional information Additional Permission

rahul mishra
  • 1,390
  • 9
  • 16
0

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

Lian
  • 1,597
  • 3
  • 17
  • 30
  • 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
0

As stated in the answer from Avinash kumar singh, editing the Twitter App additional permissions to request an email address:

edit twitter app permissions

Accordingly, a request for email is added to the authorization dialog:

twitter authorization dialog

This will result in a value being present for auth.info.email in the response hash.

Note: I am using the omniauth-twitter gem.

Akshay Narang
  • 126
  • 1
  • 3