我試圖讓OAuth2用戶通過OmniAuth工作爲我的末日應用,卻看到:
LoadError:無法加載這樣的文件 - omniauth,嘰嘰喳喳
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
的西納特拉食譜告訴我把Builder中 '配置做' WHI le官方的例子沒有,但有'使用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'
安裝了omniauth-twitter gem。 – csakon
我爲上下文添加了完整的錯誤 – csakon
它是否安裝在正確的rvm中?我在/ usr/local/rvm/gems/ruby-1.9.3-p194/gems /' –