如何根据环境设置Facebook身份验证密钥? [英] How to set facebook authentication keys depending on environment?

查看:137
本文介绍了如何根据环境设置Facebook身份验证密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用,该应用使用omniauth通过Facebook验证访问者身份.

I have a rails app which uses omniauth to authenticate visitors via facebook.

在Facebook中注册应用程序时,您必须注册一个回调网址,一旦他们完成身份验证过程,它们就会重定向到该网址.

When you register an app with facebook, you have to register a callback url which they will redirect to once they have completed their authentication process.

由于我的生产站点托管在与开发环境不同的服务器上,因此我需要在Facebook中注册2个不同的URL.为此,我注册了两个应用程序,一个是实时应用程序,另一个是开发版本.

Since my production site is hosted on a different server than my development environment, I need to register 2 different urls with Facebook. I've done this by registering two apps, a live one and a dev version.

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

Now of course I have two app_ids and two app_secrets which my Rails application needs to use.

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

How do I set OmniAuth to use one set of keys in the development environment and another in production?

我试图像下面这样在/config/environments目录中设置密钥:

I've tried to set my keys in my /config/environments directory like so:

# 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

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

and then in /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

但我知道

`method_missing': undefined method `omniauth_keys'

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

when I try to start the development server, I'm guessing because omniauth.rb is loaded before /emvironments/* ?

我该如何设置?

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

Here's a more detailed error message

=> 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>'

推荐答案

在/config/initializers/omniauth.rb

In /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

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

Note: Rails.env.production? is a Rails 3 method. I think for 2.x versions you might have to use RAIL[ENV] or something like that.

这篇关于如何根据环境设置Facebook身份验证密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆