.

Friday, November 26, 2010

Posting to Facebook from a Ruby on Rails App

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:

http://ckdake.com/content/2010/posting-to-facebook-from-a-ruby-on-rails-app.html

0 comments:

Post a Comment

Facebook Twitter Delicious Digg More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Macys Printable Coupons