Computer Beacon  

Custom Graph Apis with Facebook Graph Toolkit

Posted on 1/12/2012

Since release 2.5, the GetJsonFromFacebookObject method in FGT is made public instead of internal. This means developers can choose to interpret Facebook's response their own besides using FGT's strongly typed classes. This flexibility allows developers to use new Graph Api data types that are not implemented in the library when they're made available by Facebook. The following shows an example.

JsonObject JO = Facebook_Graph_Toolkit.Helpers.WebResponseHelper.GetJsonFromFacebookObject("me", AccessToken);

To display the returned JsonObject in a structured format, developers may call its ToHtmlString() method.

Response.Write(JO.ToHtmlString());

The result is an easy to read html.

{
    "id":"123456789",
    "name":"Secret user name......",
    .........
}

More customized Api calls

Since the JsonToolkit is a "stand alone" library itself that can be used without FGT, it can be used to parse any data that is in JSON format. The following examples shows an api call specifying the returned fields.

string Address = string.Format("https://graph.facebook.com/me?access_token={0}&fields=id,name,birthday", AccessToken);
WebClient WC = new WebClient();
JsonObject JO = new JsonObject(WC.DownloadString(Address));
string ID = JO["id"].ToString();
string Name = JO["name"].ToString();
DateTime Birthday = Convert.ToDateTime(JO["birthday"].ToString());

Thus, FGT and JsonToolkit can be used together to cover virtually every api call provided by Facebook. FGT includes most of these data types, as well as implementations to handle exceptions, test AccessTokens, decode the Base64-encoded response in Canvas Page etc. Developers however are free to extend the functionality beyond that.


You must be logged in through Facebook to write comments.

Comments

There are no comments at the moment