How to Post on App Wall as Admin from Facebook API

Java Facebook JavaScript

In this article we will use Facebook API to post automatic updates on a fan page wall on behalf of the page itself (without showing author's name). We will look into two different scenarios - posting to an application wall and posting to a separated fan page.

Main challenge in both cases would be getting permanent access token that will let us act on behalf of a page admin, the rest is very similar to making a regular wall post. Code examples will be given for JavaScript API and Java RestFB API.

Posting To Facebook Application Wall As Admin

Application wall is the one that comes "for free" with your FBML or iframe facebook application. Its URL looks like this:

http://www.facebook.com/apps/application.php?id=<APP_ID>&v=wall

Getting access token in this case is pretty simple. All we need is open the following link in a browser (as described here in "Authenticating as an Application" section):

https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=<APP_ID>&client_secret=<APP_SECRET>

This should return permanent access token we can use for posting.

In order to post on the App Wall we will need to send HTTP POST request to following connection URL:

https://graph.facebook.com/<APP_ID>/feed 

and provide message, picture, link, name, caption, description, source parameters as described here, not forgetting to send our access token parameter that we just retrieved.

Posting to App Wall using JavaScript API:

//API init code is omitted
var wallPost = {
    access_token: "<ACCESS_TOKEN>",
    message: 'Hello, World!'
};

FB.api('/<APP_ID>/feed', 'post', wallPost, function(response) {
    if (!response || response.error) {
        alert('Error occurred');
    } else {
        alert('Success!');
    }
});

Corresponding Java API code would look like:

FacebookClient facebookClient = new DefaultFacebookClient("<ACCESS_TOKEN>");
FacebookType publishMessageResponse = facebookClient.publish("<APP_ID>/feed", FacebookType.class, Parameter.with("message", "Hello, World!"));

Now if you go to your app wall you should see your message posted.

Posting To Facebook Fan Page Wall As Admin

Fan page is a page that a user has created through "Create Page" dialog. Its URL has the following format:

http://www.facebook.com/pages/<PAGE_NAME>/<PAGE_ID>?v=wall

Posting to this wall on behalf of a page admin is a bit more tricky.

Getting access token would require asking one of page admins for manage_pages extra permissions and then retrieving their "accounts" information that would contain access_token fields for every page where they have admin permissions (this process is described here in "Impersonation" section).

To achieve this we would need to run following script at our app's "Connect URL" location and ask one of page admins to go through our permission request process during facebook login:

//API init code is omitted
FB.login(function() {
    FB.api('/me/accounts', 'get', {}, function(response) {
        console.log(response);
    });
}, {perms:'publish_stream,offline_access,manage_pages'});

Now if you have Firebug console installed you should be able to explore returned response and find access_token fields for all fan pages current user is managing.

To make a Fan Page wall post all we need now is to send similar HTTP POST request as before to:

https://graph.facebook.com/me/feed

(note that it is pointing now to me/feed, not <PAGE_ID>/feed) and pass just retrieved access token among other wall post parameters:

//API init code is omitted
var wallPost = {
    access_token: "<ACCESS_TOKEN>",
    message: 'Hello, World!'
};

FB.api('/me/feed', 'post', wallPost, function(response) {
    if (!response || response.error) {
        alert('Error occurred');
    } else {
        alert('Success!');
    }
});

Corresponding Java API:

FacebookClient facebookClient = new DefaultFacebookClient("<ACCESS_TOKEN>");
FacebookType publishMessageResponse = facebookClient.publish("me/feed", FacebookType.class, Parameter.with("message", "Hello, World!"));
Jul 28, 2010
profile for serg at Stack Overflow, Q&A for professional and enthusiast programmers