LoadError:无法加载此类文件 - omniauth-twitter

时间:2013-07-31 03:19:46

标签: ruby sinatra oauth-2.0 omniauth twitter-oauth

我正试图通过OmniAuth让我的Sinatra应用程序运行OAuth2,但收到:

LoadError: cannot load such file -- omniauth-twitter


我正在关注这些例子,尽管它们有点不一致 - http://recipes.sinatrarb.com/p/middleware/twitter_authentication_with_omniauth app.rb
- https://github.com/intridea/omniauth/wiki/Sinatra-Example

Sinatra Recipe告诉我将构建器放在'configure do'中,而官方示例没有,但是'使用Rack :: Session :: Cookie'
我在寻找好的Sinatra OmniAuth示例,但它似乎更喜欢Rails。

require "omniauth"
require "omniauth-twitter"

# OAuth2 configuration
use Rack::Session::Cookie
use OmniAuth::Builder do
  provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'
end

before do
# we do not want to redirect to twitter when the path info starts
# with /auth/
  pass if request.path_info =~ /^\/auth\//

# /auth/twitter is captured by omniauth:
# when the path info matches /auth/twitter, omniauth will redirect to twitter
  redirect to('/auth/twitter') unless current_user
end

get '/auth/twitter/callback' do
# probably you will need to create a user in the database too...
  session[:uid] = env['omniauth.auth']['uid']
# this is the main endpoint to your application
  redirect to('/')
end

get '/twitter' do
  erb "<a href='/auth/twitter'>Sign in with Twitter</a>"
end

# Support for OAuth failure
  get '/auth/failure' do
  flash[:notice] = params[:message] # if using sinatra-flash or rack-flash
  redirect '/'
end


我得到的完整错误

Boot Error
Something went wrong while loading config.ru
LoadError: cannot load such file -- omniauth-twitter    /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
/Users/chadsakonchick/Projects/restlessnapkin/app.rb:11:in `<top (required)>'
config.ru:1:in `require'
config.ru:1:in `block in inner_app'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
config.ru:1:in `new'
config.ru:1:in `inner_app'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:112:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:112:in `inner_app'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:102:in `assemble_app'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:86:in `proceed_as_child'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:31:in `call!'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rack-1.5.2/lib/rack/builder.rb:138:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

2 个答案:

答案 0 :(得分:1)

看起来没有安装omniauth-twitter gem。例如:

> require "blahblahblah"
LoadError: cannot load such file -- blahblahblah

你需要gem install omniauth-twitter,如果你正在使用捆绑器,或者在项目中有意义的话,你可以将它包含在你的gemspec中。

答案 1 :(得分:1)

霰弹枪是罪魁祸首。当我从终端运行'ruby app.rb'时工作正常,但是使用霰弹枪我收到错误。