How can my facebook application post message to a wall?

ThdK picture ThdK · Apr 16, 2011 · Viewed 16.2k times · Source

i already found out how to post something to a wall with the graph api on behalf of the facebook user. But now i want to post something in the name of my application.

Here is how i'm trying to do this:

protected void btn_submit_Click(object sender, EventArgs e)
{

    Dictionary<string, string> data = new Dictionary<string, string>();
    data.Add("message", "Testing");
    // i'll add more data later here (picture, link, ...)
    data.Add("access_token", FbGraphApi.getAppToken());
    FbGraphApi.postOnWall(ConfigSettings.getFbPageId(), data);

}

FbGraphApi.getAppToken()

// ...
private static string graphUrl = "https://graph.facebook.com";
//...

public static string getAppToken() {
    MyWebRequest req = new MyWebRequest(graphUrl + "/" + "oauth/access_token?type=client_cred&client_id=" + ConfigSettings.getAppID() + "&client_secret=" + ConfigSettings.getAppSecret(), "GET");
    return req.GetResponse().Split('=')[1];
}

FbGraphApi.postOnWall()

public static void postOnWall(string id, Dictionary<string,string> args)
    {
        call(id, "feed", args);
    }

FbGraphApi.call()

private static void call(string id, string method, Dictionary<string,string> args )
{
    string data = "";
    foreach (KeyValuePair<string, string> arg in args)
    {

        data += arg.Key + "=" + arg.Value + "&";

    }

    MyWebRequest req = new MyWebRequest(graphUrl +"/" + id + "/" + method, "POST", data.Substring(0, data.Length - 1));


    req.GetResponse();  // here i get: "The remote server returned an error: (403) Forbidden."
}

Does anyone see where this i going wrong? I'm really stuck on this.

Thanks!

Answer

Frazell Thomas picture Frazell Thomas · Apr 16, 2011

You need to obtain the Auth Token for your application to post as that application.

The Auth_Token defines the security context you are posting as.

You would need to request the following Graph API URL, for the current user, to find the access token for your application.

https://graph.facebook.com/me/accounts?access_token=XXXXXXXX

This should give you an output similar to the following:

{
   "data": [
      {
         "name": "My App",
         "category": "Application",
         "id": "10258853",
     "access_token": "xxxxxxxxxxxxxxxx"
      }
   ]
}

Be sure you have the manage_pages permission before calling that API or your will not get the access token back.

Once you have the Access Token you publish to the wall like you would any other user. Note that the ID used in the URL matches the ID of the application. This will post to the Application's wall as the Application.

https://graph.facebook.com/10258853/feed?access_token=XXXXXXX

Be sure you have the publish_stream permission as well before posting to the wall.