Thursday, April 7, 2011

Post On Facebook Page As Page Not As Admin User

Sometimes, even the seemingly easy things, can actually end up being very tough. And that is what happened, when I tried to implement auto updation on my Facebook page, from my website. So, after I finally got it done, I decided to write about it here. Partly for future reference, and partly, out of an altruistic urge! :)

Before we proceed, here's my setup :
Webserver : Apache Tomcat
Framework : Java Struts
FB Utility API : restfb

And here's my problem :
I have a facebook page pageabc. I have a facebook user user123, who has administrative privileges for the page. I want to post an update on pageabc, as pageabc (not as user123). And I want this to happen, as a trigger from my webapp.

If you have not used restfb before, then I highly recommend you go through my friend Saurav's blogpost on a simple application to post updates on your Facebook profile. Its well written, it will help you set yourself up for the task, and above all, I will get paid for directing traffic to his blog! ;)

Now, if you can post a status update to your account, you are all set to move on from here. Just create a page on Facebook, set yourself up as an admin to the page and start making these changes to your code.

First up, the permissions need to be changed. For posting to a page, as a page, you need two permissions.
  • manage_pages : This will give you permission to access the data related to a user's pages
  • publish_stream : This is a basic permission, needed to update facebook status message
This information will go into your Authorize URL, and will look something like this : https://graph.facebook.com/oauth/authorize?client_id=**************&scope=manage_pages,publish_stream&redirect_uri=

Next, you need to get the pages managed by the user. The access token that Facebook provides you now, will let you access a user's pages, as well as publish to the user's feed. This can be obtained from restfb in this way :

DefaultFacebookClient fbClient;
Connection myAccounts;
fbClient = new DefaultFacebookClient(initialAccessToken);
myAccounts = fbClient.fetchConnection("me/accounts", Account.class);
You now need to parse through myAccounts, to get the names of the different pages that the user is associated with. Iterate through it, till you get the desired page. Each page will also have its own Access Token, which is associated with the permissions allowed on that page. Once you have iterated through to the page you want, get its corresponding access token (call it, pageAccessToken). This will now be used to post to the page, as the page itself.

fbClient = new DefaultFacebookClient(pageAccessToken);
fbClient.publish("me/feed", FacebookType.class, Parameter.with("message", "Aloha! ;)"));
And voila! That's it. Aloha! ;) will get posted to the page, not as an update from user123, but as an update from pageabc. The message posted this way, will successfully propagate to all the fans of the page (people who have Liked the particular page)

In conclusion :
I spent more than a couple of evenings, figuring this whole thing out. It was quite simple, but I am not very proficient in using public API's. So, it took me a little while, to get the solution. If you really liked this, feel free to send me some money over! ;) And if you really, REALLY like it, just drop in a line in the comments section!

Lets keep connecting the city.
RD,
Facilitator, CityNaksha.

Some helpful links :
http://developers.facebook.com/docs/reference/api/
http://restfb.com/
http://www.masteringapi.com/tutorials/how-to-post-on-facebook-page-as-page-not-as-admin-user-using-php-sdk/31/
http://codifyit.blogspot.com/2010/12/integrate-your-web-site-with-facebook.html

7 comments:

  1. I've been looking for a solution to the same problem and you've put me on the right track...

    Thank you!

    ReplyDelete
  2. Thanks for that. Saved me ALOT of headache :)

    ReplyDelete
  3. Merci beaucoup !! Your solution is perfect !

    ReplyDelete
  4. Mate, you're a legend, saved me from a lot of hassel... thanks!

    ReplyDelete
  5. This was great. Thank you so much.

    ReplyDelete
  6. With your help I did it in just 2 hours, you saved my lot of time. THANKS ....

    ReplyDelete