Rails 5 Devise和OmniAuth Twitter设置在授权应用

时间:2017-10-02 12:12:29

标签: ruby-on-rails devise ruby-on-rails-5 omniauth omniauth-twitter

我正在尝试在设计之上安装 OmniAuth-Twitter 。我正在关注本教程https://www.youtube.com/watch?v=X6tKAUOMzCs&t=42s,但我收到的错误是我无法在网上找到的。实际上这不是一个错误,在我从Twitter授权应用程序后,它重定向到我的sign_in表单!

omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, "ididputmykeyhere", "ididputmykeyhere"
end

的routes.rb

Rails.application.routes.draw do
  devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }

  resources :posts
  resources :categories
  root 'posts#index'
end

omniauth_callbakcs_controller.rb

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def twitter
    # Show me what I get from Twitter
    raise request.env["omniauth.auth"].to_yaml

    @user = User.from_omniauth(request.env["omniauth.auth"])
    sign_in_and_redirect @user
  end
end

devise.rb

  config.omniauth :twitter, ENV["ididputmykeyhere"], ENV["ididputmykeyhere"]

user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :omniauth_providers => [:twitter]

  has_many :posts
end

我将 user_twitter_omniauth_authorize_path 链接放置到我的视图中。

  1. 重定向到Twitter授权页面✅
  2. 我从Twitter author
  3. 授权应用程序
  4. 我希望它能够提升 request.env [“omniauth.auth”] ,但它会重定向到 localhost:3000 / users / sign_in ,我会看到登录form❌
  5. 它就像跳过我的omniauth_callbacks_controller一样,我将保存到数据库代码,我错过了什么?谢谢你!

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +

    我刚刚向我的omniauth_callbakcs_controller.rb添加了def失败,我发现该应用就是为了它。

      def failure
    
        raise request.env["omniauth.auth"].to_yaml
    
        flash[:error] = 'There was a problem signing you in. Please register or try signing in later.'
        redirect_to root_path
      end
    

    enter image description here 所以,出于某种原因,该应用程序正在跳过我的 def Twitter

1 个答案:

答案 0 :(得分:0)

太奇怪了,但删除 omniauth.rb 文件解决了这个问题。我在omniauth.rb和devise.rb 文件中定义了 API密钥。也许在多个文件中定义会导致某种冲突。