facebook
Facebook Graph API: Posting Feed To App Page As Page Revisited
August 9, 2011
0

If you have read my previous post about this subject here, then consider this a bug-fix or an update.

Posting to user’s wall is easy enough but what about if you want an application to post, say user accomplishment (such as High Scores or user achievements) onto the application page?

The process is actually straightforward. What is confusing is that an app has to use the page administrator credentials in order to post to its own page. The process of getting the credential requires a one-time ‘extraction’ of an access_token .

So here are the outline of the steps to publish as a Page. This example uses the Javascript Graph API (for an overview of using the API, you can read a tutorial here). There’s also an example Javascript page that pull access_tokens that I will describe later.

  1. Get the access_tokenof the Application, also known as app access_token.
  2. Call the publish command using FB.api(/me/feed/) or FB.ui(…) to post, while passing the access_token as one of the parameters.

1. Getting the access_token of the Application

Read this page: https://developers.facebook.com/docs/authentication/#app-login as it pretty much tells what you need to do to get the app access_token. In particular this section:

You can obtain the app access token from the Graph API token endpoint at https://graph.facebook.com/oauth/access_token by specifying your app id, app secret and client_credentials in the grant_type parameter. Both the app id and app secret are generated when your app is created in the Developer App.

https://graph.facebook.com/oauth/access_token?
     client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&
     grant_type=client_credentials

Sending an HTTP GET request to the above URL will return an access_token in the body of the response:

So head over to your Application seeting page (https://developers.facebook.com/apps/) and get the YOUR_APP_ID and YOUR_APP_SECRET.

This basically means that you can just enter something like this into the browser URL bar (assuming my YOUR_APP_ID is 201734126508882 and YOUR_APP_SECRET is 214234324234234234234)

https://graph.facebook.com/oauth/access_token?client_id=201734126508882&client_secret=214234324234234234234&grant_type=client_credentials

The browser will return the access_token, which looks something like this:


That’s YOUR_APP_ACCESS_TOKEN.

2. Posting To Page Wall

Just use the /YOUR_APP_ID/feed API to post to the Page feed. You need the have the publish_stream permission. Example code:

function postToPage()
{
  var postContent =
  {
    access_token: "YOUR_APP_ACCESS_TOKEN", // replace with the access_token that you got from the previous step
    message: userName+" has just scored "+Math.round(Math.random()*100000)+" in Cool Game",
  };
  FB.api('/YOUR_APP_ID/feed', 'post', postContent, onPostingResult);
}


function onPostingResult(response)
{
  if (!response || response.error)
  {
    alert('FAILURE: '+response.error.message);
  }
  else
  {
    alert('SUCCESS!');
  }
}

Remember to replace YOUR_APP_ID and YOUR_APP_ACCESS_TOKEN.
The result in the Fan page wall is like this:

You can also insert picture into the post, like this (do View Source in the example link below to see the parameters that I used):

To allow user to enter messages manually, use FB.ui instead of FB.api — more information can be found here.
See this for more info: http://developers.facebook.com/docs/authentication/#app-login
You can add images and links into the feed. See the parameters reference at: http://developers.facebook.com/docs/reference/api/post/

Links

Cautions

  • I do NOT know what risk is associated with exposing your APP_ACCESS_TOKEN. You have been warned.
  • An access_token can become invalid if the user who owns the access_token uninstalls the app or changes his/her password.
  • Posting may fail if you post the exact same message twice in a short period of time. I am guessing this is to prevent spamming, so if posting was successful and suddenly fails, try waiting for a while.
  • Do not spam! Do no spam!
  • All the people who have Liked your App will see post in the app page. Users may be annoyed if they see too many posts from your app and they might think your app is spammy. Facebook recently increased its spamming monitor by autobots, so you really needs to make sure that your user won’t find the posting spammy. See horror stories: http://forum.developers.facebook.net/viewtopic.php?id=103438&p=1