Ruby on Rails

A guide to using Facebook OAuth on localhost for testing during development

Running your application locally on your development machine is vital for quick feedback and easy testing. If you are are using Facebook’s OAuth as your authentication mechanism, it might get a bit more complicated to set up your application to run locally. Here’s a handy guide to getting Facebook OAuth to work even when running your app on your local development machine:

Create a new App on the Facebook Developers website

  1. Visit the Facebook Developers website and log in
  2. Click on Apps in the main menu bar at the top and then click on “Create New App”
  3. For the App Name give it a descriptive name like “<App Name> – Development”
  4. Click on Settings > Basic in the menu on the left and then under the section “Select how your app integrates with Facebook”, you will see “Website with Facebook login”. Under this section fill in Site URL with “http://localhost:3000&#8221; (or whatever port you use in your setup)
  5. Make sure to Save Changes
  6. At the very top you will see your App ID and App Secret. These will be important for the next step.

Modify the code where you initialize the Facebook App ID and App Secret

Somewhere in your code (probably in an initializer), you’re setting the App ID and App Secret for your Facebook App. Change it to look like what’s given below. I’m using OmniAuth with Devise, but this technique will work with any other Facebook OAuth setup that you are using. Wherever you are setting the App ID and App Secret, change the code to switch out a different Facebook App ID And App Secret for the development machine.

if Rails.env == &amp;amp;amp;amp;amp;quot;development&amp;amp;amp;amp;amp;quot; || Rails.env == &amp;amp;amp;amp;amp;quot;test&amp;amp;amp;amp;amp;quot;
  config.omniauth :facebook, &amp;amp;amp;amp;amp;quot;App_ID (Development Facebook App)&amp;amp;amp;amp;amp;quot;, &amp;amp;amp;amp;amp;quot;App_Secret (Development Facebook App)&amp;amp;amp;amp;amp;quot;
else
  config.omniauth :facebook, &amp;amp;amp;amp;amp;quot;App_ID (Production Facebook App)&amp;amp;amp;amp;amp;quot;, &amp;amp;amp;amp;amp;quot;App_Secret (Production Facebook App)&amp;amp;amp;amp;amp;quot;
end

Troubleshooting

  1. If you get an error like “SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed”
    Macs – http://martinottenwaelter.fr/2010/12/ruby19-and-the-ssl-error/
    Windows – https://gist.github.com/867550
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s