0

我正在尝试通过 OmniAuth 让 OAuth2 为我的 Sinatra 应用程序工作,但收到:

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-示例

Sinatra 食谱告诉我将构建器放在“配置执行”中,而官方示例没有,但有“使用 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'
4

2 回答 2

2

Shotgun was the culprit. Works fine when I run 'ruby app.rb' from the terminal, but with shotgun I receive the error.

于 2013-08-14T17:54:23.423 回答
1

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

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

如果您使用捆绑器,或者在您的项目中有意义的任何内容,您需要将gem install omniauth-twitter其包含在您的 gemspec 中。

于 2013-07-31T03:25:23.520 回答