Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 580
powerboy Avatar asked Nov 23 '12 07:11

powerboy


People also ask

Is Facebook App ID Secret?

Login Process for Facebook API: To use the Facebook API, like the Login with Facebook or Facebook Graph API, you need to create a Facebook App. When you make a Facebook App, that app will have an App ID and an App Secret.

Where to find Facebook App ID and Secret?

Click on My Apps -> Add a New App or Add a New App button. Choose Website and proceed. Step 3: Type the name of your App and click on Create New Facebook App Id, choose App category and proceed. Step 4: Now Click on Skip Quick Start. You will get the App ID and App Secret (Click Show button).


2 Answers

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.

like image 64
Ivangrx Avatar answered Oct 24 '22 08:10

Ivangrx


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!

like image 30
Henk Avatar answered Oct 24 '22 10:10

Henk