Jul 19 2009

How to update a Facebook Page Status using the Facebook API

For a client’s application, we needed to programmatically (without user intervention) update the status (wall) of a page for a company. After researching the API and several guides, you would think it was just not possible.

In fact, there’s even a forum post on the Facebook developers forum on How to update facebook page status from 3rd party application where a Facebook employee explains that it is impossible (as of 2007, at least).

The good news is, it is not impossible (as of right now). Here’s how to update the status of a Facebook page programmatically, through the API.

What you’ll need:

  • Ruby and RubyGems
  • A Facebook account
  • A Facebook page
  • A Facebook developer account or access (we’ll go through setting this up)
  • A Ruby or Rails app with access to the facebooker library (either as a gem or using the Rails plugin).

Update: As pointed out in the comments, publishing through Facebook using the method described below does place content on the fan page, however, it is not displayed in any user’s feeds or streams, which makes it not quite so useful. We have since opted to go with the uber-cool ping.FM service and we even wrote a little ping.fm ruby wrapper library for their API.

Facebook’s API is really meant for the web — it’s a session-based API that relies on a browser being opened, being redirected to the Facebook site, and authorization happening in their walled-castle.

Just for reference, and to show the differences of posting a tweet through the Twitter API (with the use of a Twitter Ruby gem for a truly fair comparison):

1) Authorize using HTTPAuth:

 Twitter::HTTPAuth.new('yourusername', 'yourpassword') 

2) Create the “post” (or “tweet”):

 twitter_client = Twitter::Base.new(httpauth) twitter_client.update("this is a post to twitter!") 

Very easy, and very straightforward. Sure, there’s some abstraction hidden here, but it’s an HTTP POST of XML or JSON to a status update action, and an XML or JSON response. It’s truly stateless, and can be done in one call, you could even do this using curl (an http library) in one-line.

OK, now that we’ve seen how easy it could be, let’s do the equivalent on Facebook.

To post an update to a Page with Facebook, it requires a bit more prep work.

0) Signup as a Developer, by visiting:

http://developers.facebook.com/get_started.php

Click the button to “Add the Facebook Developer App” which is confusing, but cool that being a Developer on Facebook requires you to install the “Developer” Application:

You will see a interstitial, click allow:

1) Create an Application. You can name it the same as your Page:

Here are the steps you definitely need to complete:

Grab your API key and secret key (not shown in this screen capture):

In the Authentication Settings, make sure this application is installable to Pages:

In the settings, create a Canvas page URL, even though we won’t need it:

You can put in a Canvas Callback URL, but it doesn’t matter.

2) Publish your page, and your application. Now visit the profile page for the Application:

Add the Application to the Page:

You may have to click the “More” actions tab to find the link to “Add to my Page.”

3) Let’s generate a “token” which is the equivalent of an “infinite” session that will allow us to programatically make calls to the API, even if we’re not actively logged in as the owner of the Page. The procedure is documented here: http://www.emcro.com/blog/2009/01/facebook-infinite-session-keys-no-more/

I’m sure this could be done through the API itself, but since it’s a one-time thing, we can do it ourselves, through our browser.

3a) First, log out of Facebook and hit this URL:

 http://www.facebook.com/login.php?api_key=YOUR_API_KEY 

Note that you will be redirected to your canvas page with a auth_token parameter:

3b) Now, visit this page to generate the token:

 http://www.facebook.com/code_gen.php?v=1.0&api_key=YOUR_API_KEY 

You should see your new auth_token on the next page:

3c) Now, you can generate a session using this token that should never expire.

Here’s how to do that using the Facebooker library for Ruby. In script/console:

 fb_session = Facebooker::Session.create # => #<Facebooker::Session:0x330cf0c ....  fb_session.post "facebook.auth.getSession", :auth_token => "YOUR_GENERATED_TOKEN" # => {"session_key"=>"xxxxxxxx", "expires"=>"0", "uid"=>"yyyyyy"} 

If you look at the session returned, you’ll see a session_key and see that the “expires” is set to 0.

If you have a library that lets you “set” the session key, you’re good to go. You can use that session_key until it is revoked (since it does not expire).

If you’re using the Facebooker library, since it has been built to “authorize” users through the web and store a facebook session along side a normal web/app session, you actually can’t do this. In the library, the session_key is an attr_reader (meaning, a read-only attribute for the Facebooker::Session instance).

And even though the guide above (facebook-infinite-session-keys-no-more) tells you to use that session_key, it is perfectly valid (and I assume more appropriate) to just use the auth_token instead, every time.

So, now we’re down to this for logging in:

 fb_session = Facebooker::Session.create fb_session.auth_token = "YOUR_GENERATED_TOKEN" 

Note, if you’ve setup Facebooker using the plugin, it loads your api key and secret key using the configuration yaml file. You could also be explicit:

 fb_session = Facebooker::Session.create(Facebooker.api_key, Facebooker.secret_key) fb_session.auth_token = "YOUR_GENERATED_TOKEN" 

4) Now, time to post something to the page. We’ll use the Facebook API feed.publishTemplatizedAction method, using the page_actor_id parameter. As of right now, the documentation states correctly that this method “is deprecated for calls made on behalf of users. This method works only for publishing stories on a Facebook Page that has installed your application.”

Here’s a sample call in Ruby:

 fb_session.post("facebook.feed.publishTemplatizedAction", :target_ids => "",  :title_template => "Testing a News Feed post on a Page posted by {actor}",  :title_data => "{}",  :body_template => "Check it out yo says {name}.",   :body_data => "{\"name\": \"cbq\"}",   :body_general => "Here is the body.",   :page_actor_id => "YOUR_PAGE_ID") 

A couple of things to note. Facebook requires that you put the {actor} variable in the title, so you’ll have to come up with something clever to do with that. You can also put data in the title for replacement, just like you can do with the body. Above, the example has a {name} variable replaced in the body, but you don’t have to add any body data to replace. The above example shows passing variables and not passing variables for reference.

The page id should be easy to grab from any of the URLs of your Page.

You should be all set, and see your posts on your page momentarily:

References:

1 Comment

  1. Arun Prasath

    Nice tutorial bro. Thanks for sharing! :)

Leave a Comment

Join the discussion. Do not worry, your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>