Use omniauth-facebook when app id and app secret are different for each request?

╄→гoц情女王★ 提交于 2019-12-18 13:05:58

问题


The omniauth-facebook README mentions how to set it up in an initializer and how to set options like scope per request only. I wonder if it is possible to set app id and app secret per request as well.


回答1:


You can do this:

On your omniauth.rb, do this:

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

Then on your controller you have to define the following:

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

Of course you have to add the associated routes, on your routes.rb.

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

Good luck

Regards. Ivan.




回答2:


I use devise (followed this railscast: http://railscasts.com/episodes/235-devise-and-omniauth-revised), but it took me a while to understand how to implement Ivangrx' solution. It turned out to be quite easy. Here's my code:

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

Thanks for the help on this one!




回答3:


If you are using devise you can do this

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
    }


来源:https://stackoverflow.com/questions/13524919/use-omniauth-facebook-when-app-id-and-app-secret-are-different-for-each-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!