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.

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.

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.”

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.

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.