Search

Powered By

Advertisements

Posting to Facebook from a Ruby on Rails App

Posted at: 2010-11-02 14:40:51 -0400
Once you get your head wrapped around OAuth and Facebook's specific implementation of it, this isn't too bad. That said, it took me a little while to get this figured out and there wasn't a good example of this specific use case, so here goes!
You should definitely checkout the Facebook Graph API Overview as it explains all the things you can do in detail. Here, I just connect with OAuth with the "offline_access" and "publish_stream" permissions which lets this application post things to Facebook without requiring any further input from the user or requiring them to be active on the site. (The site I wrote this for let's users schedule something to be posted, so it needs to be able to post at that time regardless of if the user is around or not.) You will need to have registered an application on Facebook to do this. The basic application flow:

User visits facebook_account/new when they want to link in Facebook

They get redirected to the allow/deny page on Facebook that prompts the user to log in and accept your application

Assuming they accept, they get redirected back to callback/facebook/#{ID} with a "code" paramater (The oauth verification token)

The application sends that code back to Facebook to retrieve an access_token, which can be used for all future posts to Facebook to act as the user

Assuming you're using Rails 3, everything below is stubbed out and may work out of the box, but you should use it as an example and write code yourself that does error handling, etc. The code in my actual project is a lot more comprehensive but I trimmed it out so that the basic flow here is easier to understand. Don't just copy/paste this all and expect it to work! No warranty is provided. The Sun may explode at any time.
First get the gems you'll need:
and run "bundle install".
Then set up some routes:
And create a table in a migration to hold the information. Run "rails generate migration CreateFacebookAccount" and put this in the file it creates for you:
The model takes care of building the authorize URL, validating the token we get back, and posting:
While the controller redirects when needed and handles the callback from facbeook:
And thats it! Anywhere else in your account you can call .post('Hello World') on an active FacebookAccount and it'll show up on the linked user's Facebook wall.