Rails - Devise + OmniAuth:如何设置暂存版本的凭据?

时间:2013-08-07 12:32:26

标签: ruby-on-rails ruby devise omniauth

/config/initializers/devise.rb 中,我有类似这样的内容:

  # production
  config.omniauth :facebook, 'aaa', 'bbb',
      :site              => 'https://graph.facebook.com',
      :authorize_path    => '/oauth/authorize',
      :access_token_path => '/oauth/access_token',
      :scope => 'email'

  # staging version
  config.omniauth :facebook, 'ccc', 'ddd',
      :site              => 'https://graph.facebook.com',
      :authorize_path    => '/oauth/authorize',
      :access_token_path => '/oauth/access_token',
      :scope => 'email'

当我将这2个代码块放入 devise.rb 文件时,我收到错误消息,指出存在错误的凭据。

我不知道在Twitter和Facebook等设备上设置OmniAuth凭据的最佳方法是什么 - 我使用的那个显然是不正确的。

为应用的localhost,制作和暂存版本设置凭据的最佳方法是什么?

由于

3 个答案:

答案 0 :(得分:1)

您的凭据似乎对localhost有误。我有两个版本的信用证用于开发和生产,这里是示例

if Rails.env.development?
    config.omniauth :facebook, "xxx", "yyy"
    config.omniauth :vkontakte, "xxx_loc", "yyy_loc"
else
    config.omniauth :facebook, "zzz", "rrr"
    config.omniauth :vkontakte, 'zzz_loc', 'rrr_loc'
end

at /config/initializers/devise.rb

答案 1 :(得分:0)

case Rails.env
when "production"
    # production version
    config.omniauth :facebook, 'aaa', 'bbb',
            :site              => GRAPH_URL,
            :authorize_path    => '/oauth/authorize',
            :access_token_path => '/oauth/access_token',
            :scope => 'email'
when "staging"
    # staging version
    config.omniauth :facebook, 'ccc', 'ddd',
            :site              => GRAPH_URL,
            :authorize_path    => '/oauth/authorize',
            :access_token_path => '/oauth/access_token',
            :scope => 'email'
else
    # development version
    config.omniauth :facebook, 'eee', 'fff',
            :site              => GRAPH_URL,
            :authorize_path    => '/oauth/authorize',
            :access_token_path => '/oauth/access_token',
            :scope => 'email'
end

答案 2 :(得分:0)

处理不同凭据的最佳方法是将它们放在环境变量中,就像omniauth gem doc所说:

https://github.com/intridea/omniauth#getting-started

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :developer unless Rails.env.production?
  provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
end

这种方法有几个优点:

  1. 保持您的配置简单
  2. 代码库中没有凭据
  3. 并不限制您使用dev / test / prod