簡體   English   中英

“omniauth-twitter”電子郵件ID不是來自ruby on rails的twitter

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

我正在使用omn​​iauth-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現在通過此特別許可表格提供電子郵件。 填寫請求特殊權限的表單並等待批准。

您還可以查看此答案以獲取更多信息。

Omniauth gem獲取用戶的電子郵件

請參閱: include_email = true

您必須啟用其他信息 額外許可

GEM工作正常,問題是Twitter因某些原因不return email 不像facebook ..

如Avinash kumar singh的回答中所述,編輯Twitter App請求電子郵件地址的其他權限:

編輯Twitter應用權限

因此,在授權對話框中添加了電子郵件請求:

twitter授權對話框

這將導致響應哈希中的auth.info.email存在一個值。

注意:我使用的是omniauth-twitter gem。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM