September 13, 2010
Single Sign On via Facebook on Rails

I’ve been looking for the simplest method by which new visitors can authenticate at spoonflower using their facebook ids.

Let me start by saying that researching this has been really confusing.  Facebook is in the midst of major transitions/simplifications to its public interfaces,  including changing focus away from FBML.  When I investigated this awhile ago I ended up looking into the facebooker gem.  This is an automation method that uses the REST API, and is now unsupported because a lot of the code was devoted to supporting fbml, and was not compatible with Rails 3. 

Its been replaced by facebooker2, a new plugin based on mogli that works with all versions of rails and is a lot smaller, as no effort is made to support fbml.  However, not much doc has been drafted, so we’ll see how it goes.

I’m going to tackle this in three parts:

1) get the facebooker2 plugin to work in my environment - hello world.

2) Use facebooker2 to log in to facebook from my local app, and then retrieve email address and facebook user id.

3) Use the facebook account data to create a paired local account, enable a returning user to go straight into their new Spoonflower account on their return.

Here goes!

—————-

I am working from the README on the facebooker2 project: 

http://github.com/mmangino/facebooker2

and also drawing from a complete working example that is at: 

http://github.com/mmangino/facebooker2_fb_connect_example

The first step is to set up my facebook app and make sure I can work with it on localhost, so I can play with this stuff on my desktop.  The key here is, once your app is created, to edit your website tab.

Go to http://www.facebook.com/developers/editapp.php?app_id=[Application ID]

and click on the ‘web site’ tab.  Under ‘Site Domain’ enter ‘localhost’, and under ‘Site URL’, enter ‘http://localhost/’, and don’t leave off the trailing slash!

After setting up my facebook app, I set about installing the facebooker2 plugin and the mogli gem.

script/plugin install git://github.com/mmangino/facebooker2.git

sudo gem install mogli

….which brought aboard 4 gems:

When you HTTParty, you must party hard!
Successfully installed hashie-0.4.0
Successfully installed crack-0.1.8
Successfully installed httparty-0.6.1
Successfully installed mogli-0.0.14

Steps 2-5 verbatim:

2. Create config/facebooker.yml with the appropriate environment.

production:
  app_id: <your application id>
  secret: <your application secret>
  api_key: <your application key>

3. Create config/initializers/facebooker2.rb and place the following line in it

Facebooker2.load_facebooker_yaml

4. Add the following line to your app/controllers/application_controller.rb

include Facebooker2::Rails::Controller

Pretty Simple.

The next (and final) step on the facebooker2 readme requires some thought:

5. Update your rails applications to use the rails helpers. This could be in a shared login partial.

I went ahead and created a page with the readme example:

<%= fb_connect_async_js %>

<% if current_facebook_user %>  

<%= "Welcome #{current_facebook_user.first_name} !" %>

<%= fb_logout_link("Logout of fb", request.url) %><br />

<% else  %>
<%= fb_login_and_redirect('fb', :perms => 'email') %>
<% end %>

And fired up the browser.  The first issue:

no such file to load — hmac-sha2

A quick google revealed another developer with the same issue, and it looks like they are on the same versions as me:

Rails      -   2.3.4 
Ruby      -  1.8.7

It turns out that I’m missing a gem: ‘ruby-hmac’, so I do a

sudo gem install ruby-hmac

Successfully installed ruby-hmac-0.4.0

Okay! and try again and

 The application spawner server exited unexpectedly

Exception class:
    Passenger::Railz::ApplicationSpawner::Error

hmmm.  Hunted down my error logs (they are at /opt/local/apache2/logs on my OSX box) and found this:

Exception Facebooker2::NotConfigured in Passenger::Railz::ApplicationSpawner (Unable to load configuration for development from facebooker.yml. Is it set up?) (process 2424):
    from /Users/gartdavis/spoonflower/vendor/plugins/facebooker2/lib/facebooker2.rb:29:in `load_facebooker_yaml’
    from /Users/gartdavis/spoonflower/config/initializers/facebooker2.rb:1

That makes more sense.  When I created my facebook.yml I copied from the Readme verbatim, so the environment in my .yml file that is supported is ‘production’ and I’m running ‘development’ on my localhost.  I create a separate entry for ‘development’ with my facebook identifiers and try again, and this time my localhost root page comes back properly. 

I hit my new facebook page and get a facebook login link.  End of Step 1!

Onwards to step 2: http://tumblr.com/xfbijzyvy

  1. gartdavis posted this