简体   繁体   中英

Issue with omniauth-twitter callback

I am providing a "login with twitter" link on /auth/twitter with the omniauth gem.

everything seems to be fine when i run project in development environment. But i get an error while trying to log in in production.

after i run rails s -e production and click 'sign in with Twitter' button

Started GET "/auth/twitter" for 127.0.0.1 at 2014-08-25 09:42:15 +0200 I, [2014-08-25T09:42:15.959278 #5144] INFO -- omniauth: (twitter) Request phase initiated.

Started GET "/auth/twitter/callback?oauth_token=BnjEPgOdMrOMMSE**************&oauth_verifier=mXD2WICHMs1k6z04UJ***********" for 127.0.0.1 at 2014-08-25 09:42:17 +0200 I, [2014-08-25T09:42:17.920926 #5144] INFO -- omniauth: (twitter) Callback phase initiated.

Processing by SessionsController#create as HTML Parameters: {"oauth_token"=>"lUv4mg4F*************", "oauth_verifier"=>"l0i8DN********", "provider"=>"twitter"}

SQLite3::SQLException: no such table: users: SELECT "users".* FROM "users" WHERE "users"."provider" = 'twitter' AND "users"."uid" = '27*****' ORDER BY "users"."id" ASC LIMIT 1

i tried to add users table to db/migrate manualy and run rake db:migrate, but that didn't work.

my Gemfile file:

source 'https://rubygems.org'


gem 'rails_12factor'
gem 'spork'

gem 'omniauth-twitter', '~> 1.0.1'

gem 'rb-readline'

gem 'rspec-rails'
gem 'rspec'

gem 'bootstrap-sass'

gem 'bootstrap'

gem 'twitter-bootstrap-rails'

gem 'twitter-text'

gem 'twitter', '~> 5.3.1'

gem 'sqlite3'

gem 'sass-rails', '~> 4.0.3'

gem 'uglifier', '>= 1.3.0'

gem 'coffee-rails', '~> 4.0.0'

gem 'jquery-rails'
gem 'turbolinks'

gem 'jbuilder', '~> 2.0'

gem 'sdoc', '~> 0.4.0',          group: :doc

gem 'spring',        group: :development

my config/initializers/omniauth_twitter.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, Rails.application.config.twitter_key, Rails.application.config.twitter_secret
end

and that's sessionsController#create

class SessionsController < ApplicationController
  def create
    begin
    user = User.from_omniauth( env["omniauth.auth"] )
    session[:user_id] = user.id
    redirect_to root_path
    rescue => ex
    logger.error ex.message
    end
  end
  ...
end

and User.from_omniauth

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_secret = auth.credentials.secret
     user.save!
    end
  end

您是否在生产环境中进行迁移?

rake db:migrate RAILS_ENV=production

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM