Computer Beacon  

Get Started

Facebook Graph Toolkit is a powerful and rich library that provides flexibility and functionality to build Facebook Applications for Iframe Canvas Applications or external websites. using Visual Basic or C# in ASP.NET.
Facebook Graph Toolkit

Latest News

FGT Update v3.1

Posted on 5/7/2012

This update fixes a few bugs and adds a few features reported / suggested by developers. You can grab the library at the FGT CodePlex project.

  • New user relationships are added, suggested by Tony Gravagno
  • Incorrect url when posting link attachments with urls that contain the '&' character, reported by Roberto Minoia
  • Incorrect IsAuthorized property, also reported by Roberto Minoia
  • GetGrantedPermissions() is now available on the Api class directly, not restricted to CanvasPage and TabPage class, suggested by Easton Harvey

Other updates include improved error handling when Facebook return "403 forbidden", fixed bug when posting unicode characters in posts and ships with JSON Toolkit v3.1 which offers additional speed up.

Thanks everyone for your support and positive feedbacks on this library, it has been doing very well. FGT v4 is currently at planning stage; its expected to implement Open Graph, a new and more flexible authorization model and hopefully expand to other platforms as well. If you have any suggestions for the future release you can post to our Facebook Page, send an email or comment below.


Further optimizations in JSON Toolkit v3.1 update

Posted on 3/28/2012

About a month ago I released JSON Toolkit v3.0 which boosted the parsing speed up to 10x times. Today I just released v3.1 update which brings further performance improvement to the parsing operation. This time the speed-up is not that big though, about 5% to 10% (-:

Sample Data v3.1 v3.0 Speed-up
Serialized Java servlet 3700 ms 3943 ms 6.56%
Bing Api search response 1188 ms 1251 ms 5.27%
Facebook Graph object 567 ms 618 ms 8.85%

A new JsonException class has been added to better distinguish exceptions that are thrown by the toolkit.

There is no change to functionality, so there is no need to re-write any code. Just download the dll file from CodePlex and re-compile.


How to write a Facebook Page Tab app in 10 minutes

Posted on 3/19/2012

In this article I'll show how simple it is to write a Facebook app for Page Tabs using Facebook Graph Toolkit (-:

First thing first go to https://developers.facebook.com and create a new app. Copy the AppID and Secret to web.config:

<configuration>
    <configSections>
        <section name="FacebookGraphToolkitConfiguration" type="Facebook_Graph_Toolkit.FacebookGraphToolkitConfiguration"/>
    </configSections>

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <FacebookGraphToolkitConfiguration AppID="xxxxxxxxxx" AppSecret="xxxxxxxxxxxxxx">
    </FacebookGraphToolkitConfiguration>

</configuration>

Next, go to the app settings page on Facebook, and check the Page Tab function. Give your app a fancy name. Point the Page Tab URL and Secure Page Tab URL to an address that contains the aspx page you'll be creating. Localhost is also acceptable. Now here's the problem: Facebook requires apps to accept https connection. Normally enabling sandbox mode will temporarily allow normal http connection, but this doesn't work for Page Tabs (most probably Facebook bug). ScottGu's Blog has a nice article that explains how to create local SSL certificates for development purpose. When you deploy the application of course, you will need to purchase one for your server.

Here I'm going to create a very simple app called "Sum your name" - it basically just sums the ASCII (or unicode) values of the characters in your name and displays it. This is the same sample that is used for the Canvas and Page Tab demo at https://apps.facebook.com/aspdotnetsample/ .

To access the user's name we need the user to authorize our app. So let's put the below html into our .aspx page:

<div id="div_Authorized" runat="server">
    <p>Your name is: <asp:Label ID="Label_name" runat="server" /></p>
    <p>The sum of the ASCII values of your name is: <asp:Label ID="Label_Sum" runat="server" /></p>
</
div>
<
div id="div_NotAuthorized" runat="server">
    <p>You have not authorized the app.</p>
    <asp:Button runat="server" Text="Authorize" OnClick="AuthClick" />
</
div>

People often have questions about how to display different content for users who have and have not authorized the app. The simplest solution is render everything in a single .aspx page. After all, it's a server-side script, so all it matters is delivering the right html to the user. Do not use IframeRedirect, Response.Redirect or <a> links, because the information that is POSTed by Facebook will get lost in this process.

The C# code behind this page is quite straight forward:

public partial class PageTab : Facebook_Graph_Toolkit.TabPage {
    protected void Page_Load(object sender, EventArgs e) {
        if (Api == null) div_Authorized.Visible = false;
        else {
            div_NotAuthorized.Visible = false;
            Facebook_Graph_Toolkit.GraphApi.User u = new Facebook_Graph_Toolkit.GraphApi.User("me", Api.AccessToken);
            string name = u.Name;
            UInt64 sum = 0;
            foreach (char c in name) sum += c;
            Label_Sum.Text = sum.ToString();
            Label_name.Text = name;
        }
    }
    protected void AuthClick(object sender, EventArgs e) {
        RedirectToFacebookAuthorization();
    }
}

  1. Inherit TabPage instead of System.Web.UI.Page
  2. If the Api object is null, the user has not authorized our app.
  3. Call the RedirectToFacebookAuthorization() method as necessary. After authorization, the user will be redirected to this page again by Facebook.

Final thing, we need a mechanism that would allow Page administrators to add our app to their pages. This is achieved by redirecting the administrator to an address obtained by calling Dialog.GetAddPageTabUrl() method. You can do this at anywhere you like, it can be a website, it can be Canvas app, or a link in an email. Below is an example of how to achieve this in a canvas app:

FacebookAppConfig c = FacebookAppConfig.FromWebConfig;
string url = Dialog.GetAddPageTabUrl(c.AppID, c.CanvasAddress + "Default.aspx", DialogDisplayType.FullPage);
Facebook_Graph_Toolkit.Helpers.IframeHelper.IframeRedirect(url, false, true);

If it is just a normal ASP.NET webpage, you can put the link in an <a> tag or use Response.Redirect. If you get an error from Facebook saying that the url address must be owned by the app, simply go to the app settings and add your domain name there.

Finally, publish your app. And that's it!