如何根据环境设置facebook身份验证密钥?

Dav*_*ite 6 facebook ruby-on-rails omniauth ruby-on-rails-3

我有一个rails应用程序,使用omniauth通过Facebook验证访问者.

当您在Facebook上注册应用程序时,您必须注册一个回调URL,一旦完成身份验证过程,他们将重定向到该回调URL.

由于我的生产站点托管在与我的开发环境不同的服务器上,因此我需要在Facebook上注册2个不同的URL.我通过注册两个应用程序,即实时版本和开发版本来完成此操作.

当然,我有两个app_ids和两个app_secrets,我的Rails应用程序需要使用它们.

如何将OmniAuth设置为在开发环境中使用一组密钥而在生产环境中使用另一组密钥?

我试图在我的/ config/environments目录中设置我的密钥,如下所示:

# development.rb
SampleApp::Application.configure do
  config.omniauth_keys.facebook.app_id = "32423423"
  config.omniauth_keys.facebook.api_key = "324234324234324"
  config.omniauth_keys.facebook.app_secret = "242343424234324324234324"
end

# production.rb
SampleApp::Application.configure do
  config.omniauth_keys.facebook.app_id = "7657566"
  config.omniauth_keys.facebook.api_key = "7756765765756757"
  config.omniauth_keys.facebook.app_secret = "65756767567567567757"
end
Run Code Online (Sandbox Code Playgroud)

然后在/config/initializers/omniauth.rb中

Rails.application.config.middleware.use OmniAuth::Builder do 
  keys = SampleApp::Application.config.omniauth_keys

  # ==> OmniAuth Provider Credentials
  #                   APP_ID                SECRET_ID
  provider :facebook, keys.facebook.app_id, keys.facebook.app_secret
end
Run Code Online (Sandbox Code Playgroud)

但我明白了

`method_missing': undefined method `omniauth_keys'
Run Code Online (Sandbox Code Playgroud)

当我尝试启动开发服务器时,我猜是因为在/ emvironments/*之前加载了omniauth.rb?

我怎么设置它?

编辑:这是一个更详细的错误消息

=> Booting WEBrick
=> Rails 3.0.4 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/railtie/configuration.rb:77:in `method_missing': undefined method `omniauth_keys' for #<Rails::Application::Configuration:0xadaa69c> (NoMethodError)
    from /home/david/sites/sample_app/config/environments/development.rb:32:in `block in <top (required)>'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/application.rb:47:in `class_eval'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/application.rb:47:in `configure'
    from /home/david/sites/sample_app/config/environments/development.rb:1:in `<top (required)>'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `require'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `block in require'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `require'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/application/bootstrap.rb:11:in `block in <module:Bootstrap>'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/initializable.rb:25:in `instance_exec'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/initializable.rb:25:in `run'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/initializable.rb:49:in `each'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/initializable.rb:49:in `run_initializers'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/application.rb:134:in `initialize!'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/application.rb:77:in `method_missing'
    from /home/david/sites/sample_app/config/environment.rb:5:in `<top (required)>'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `require'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `block in require'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:239:in `require'
    from /home/david/sites/sample_app/config.ru:3:in `block in <main>'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
    from /home/david/sites/sample_app/config.ru:1:in `new'
    from /home/david/sites/sample_app/config.ru:1:in `<main>'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/server.rb:65:in `start'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands.rb:27:in `tap'
    from /home/david/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
Run Code Online (Sandbox Code Playgroud)

Mat*_*ani 17

在/config/initializers/omniauth.rb中

if Rails.env.production?
   # set the app parameter
elsif Rails.env.development?
   # set the app parameter
else  
   # test env
   # set the app parameter
end
Run Code Online (Sandbox Code Playgroud)

注意:Rails.env.production?是一个Rails 3方法.我认为对于2.x版本,您可能必须使用RAIL [ENV]或类似的东西.