【问题标题】:Use omniauth-facebook when app id and app secret are different for each request?当每个请求的应用程序 ID 和应用程序密码不同时使用omniauth-facebook?
【发布时间】:2012-11-23 07:46:54
【问题描述】:

omniauth-facebook README 提到了如何在初始化程序中进行设置,以及如何仅针对每个请求设置scope 等选项。我想知道是否可以为每个请求设置应用程序 ID 和应用程序密码。

【问题讨论】:

  • 我想知道你为什么要为每个请求设置应用程序 ID 和应用程序密码。每个应用只有一个键。
  • 如果我的网站需要支持多个 Facebook 应用程序怎么办?想想 SaaS。

标签: ruby-on-rails omniauth


【解决方案1】:

你可以这样做:

在您的 omniauth.rb 上,执行以下操作:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook,:setup => true
end

然后在您的控制器上,您必须定义以下内容:

def setup
 request.env['omniauth.strategy'].options[:client_id] = @site.facebook_key
 request.env['omniauth.strategy'].options[:client_secret] = @site.facebook_secret
 render :text => "Setup complete.", :status => 404
end

当然,你必须在你的 routes.rb 中添加相关的路由。

  #Facebook Omniauth routes
  match '/auth/facebook/callback' => 'session#authorize_callback'
  match '/auth/facebook/setup' => 'session#setup'

祝你好运

问候。 伊万。

【讨论】:

  • 所以它会为每个身份验证过程再向“/auth/facebook/setup”发出一个请求?为什么:status => 404
  • 以及如何配置OmniAuth.config.full_host
  • 我得到了这个工作,但在 Twitter 上也没有......我设置了两个 setup 方法,但似乎omniauth-twitter 没有相同的功能?
  • Kyle,我不确定这是否有帮助,但对于 twitter,您必须将“request.env['omniauth.strategy'].options[:client_id]”替换为“ request.env['omniauth.strategy'].options[:consumer_key]”和“request.env['omniauth.strategy'].options[:client_secret]”到“request.env['omniauth.strategy']。选项[:consumer_secret]"。
【解决方案2】:

我使用 devise(关注此 railscast:http://railscasts.com/episodes/235-devise-and-omniauth-revised),但我花了一段时间才了解如何实施 Ivangrx 的解决方案。事实证明这很容易。这是我的代码:

# /config/initializers/devise.rb
config.omniauth :facebook, setup: true

# routes.rb
devise_scope :user do  
  #where omniauth_callback is the controller I made when following the Railscast
  get "users/auth/facebook/setup" => "omniauth_callbacks#setup"
end

# omniauth_callbacks_controller.rb
def setup
  request.env['omniauth.strategy'].options[:client_id] = @site.facebook_id
  request.env['omniauth.strategy'].options[:client_secret] = @site.facebook_key
  render :text => "Setup complete.", :status => 404
end

感谢您对此的帮助!

【讨论】:

  • 是否可以使用相同的设置函数来处理多个策略(例如:facebookgoogle_oauth2)。
【解决方案3】:

如果你正在使用设计,你可以这样做

config.omniauth  :facebook, :setup => lambda{
      current_app_secret = // Get current domain
      current_app_key = // Get config
      env['omniauth.strategy'].options[:client_id] = current_app_secret
      env['omniauth.strategy'].options[:client_secret] = current_app_key
    }

【讨论】:

  • 这段代码在哪里?初始化器?如果需要从数据库中获取current_app_secret 怎么办?将 ActiveRecord 代码放在初始化程序中看起来很难看,即使它可以工作。
  • 我认为您也应该能够在应用程序控制器中覆盖它们。您可以在 application_controller 中编写 before_filter。让我知道会发生什么。
  • 谢谢!但实际上我没有使用设计。我的网站不需要设计提供的任何功能。
  • 它对我不起作用:/users/auth/facebook 重定向我,我得到No route matches [GET] "/oauth/authorize"
猜你喜欢
  • 2015-03-21
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多