omniauth-facebook自述文件提到了如何在初始化程序中设置它以及如何仅为每个请求设置scope
等选项。我想知道是否可以为每个请求设置app id和app secret。
答案 0 :(得分:17)
你可以这样做:
在你的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'
祝你好运
的问候。 伊凡。
答案 1 :(得分:5)
我使用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
感谢您对此的帮助!
答案 2 :(得分:1)
如果你正在使用设计,你可以这样做
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
}