Author image
Senior Consultant

Creating Facebook Canvas Apps & Page Tabs

Facebook integration is obviously starting to become a de facto requirement with most web development projects. In most cases the requirements are reasonably straight forward and involve nothing more than including a ‘Like’ button on content, but what about actually adding your own bespoke content to Facebook like The Guardian – bring on the Facebook Canvas App.

Canvas Apps are essentially just a way of wrapping some externally hosted content and putting this onto a Facebook branded page. We’ll use a basic example of just getting this page onto Facebook as a starting point before starting to explore how we can tie this in with the Facebook GraphAPI and Open Graph Protocol to provide a rich app all written in a language which you are comfortable with. Gone are the days when we need to use the clunky Facebook Markup Language (FBML) and we can opt for the much simpler approach of iFrames leaving us as developers to concentrate on developing our apps and not having to plough through loads of poorly documented Facebook technical jargon.

Becoming a Facebook Developer

Before we do anything we need to get ourselves setup with a Facebook developers account. I am working from the ground up, having created a brand new Facebook account just to make sure I don’t miss any steps out, I’m pretty sure this will go against the Facebook terms of use but will dutifully delete the account when I’m done with this article!

First of all we need to visit the Facebook Developers Center which can be found at http://developers.facebook.com/ it is worth noting here that this is where you’ll find a whole load of documentation about how to use the various Facebook APIs. From here you need to click ‘Apps’ in the top right corner which will then ask you to give your permission to connect as shown below.

Developer App Authentication

Once you've given this the nod you'll be set up as a Facebook developer.

Creating your App in Facebook

We now need to create our App in Facebook, since the actual mechanics of the App will all be contained in the page that you display within the Canvas we'll ignore the inner workings for the time being and just make a simple App that'll render this page in Facebook. Start by clicking 'Create New App'. This will present you with a modal window to collect some basic information about your new App, you only really need to give it a valid name, however, it is worth giving it a namespace as well. The namespace will become useful when you start to play with the Open Graph Protocol.

Developer App Authentication

Click 'Continue' and once you've successfully entered a CAPTCHA you'll get to a whole load of settings for you app. We're interested in the 'Select how your app integrates with Facebook' at the bottom of the 'Basic Settings' page as shown below:

Basic App Settings

We need to do three things here, firstly we'll create a Canvas App which will just pull this page through to an iFrame within Facebook and also a Page Tab so that we can add a tab to it on our Facebook Page - we'll explore the concept of the Page Tab a bit more later. Finally we need to add a Site URL under the 'Website' tab, this is to give our app the context of a domain. Once we're done the settings will look similar to those shown below. It is worth noting that we need to supply both a standard URL and a secure URL as Facebook won't display content that isn't encrypted if the user is connected via HTTPS so that it doesn't cause an SSL certificate warning.

Completed App Settings

Using your app

Once you've set your app up that's it - you're ready to start using it. You can view you Canvas App by just using the URL provided by Facebook which in our example is http://apps.facebook.com/computermindscanvas/ you will notice that this doesn't look brilliant mainly because we're just rendering out this website and this hasn't been designed to fit into the space available. But this is exactly the way that popular Apps such as The Guardian news app work - we'll get onto the other clever Graph stuff in a bit.

We can also get our app to be displayed as a tab on a Facebook page, so for example if you visit our Facebook Page you'll notice we have a tab linking to this article as shown below.

Facebook Page Tab

To get this in place you need to visit the following URL where YOUR_APP_ID is the generated ID of your app which can be found at the top of your app's settings page and YOUR_URL is the URL that you want to redirect to once your Page Tab has been added. This has to be a URL that is of the same domain as your app's Site URL which we set up earlier.

http://www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID&next=YOUR_URL

So long as you are logged into Facebook you will then get the follow dialog which will allow you to choose which page you want to add the Page Tab to. Select a Page from the drop down and click 'Add Page Tab' and then you're done, you should now have a page tab on the selected Facebook page which pulls through content from the URL specified in your app's settings.

Adding the Page Tab

The Facebook GraphAPI and Open Graph Protocol

Now this all may seem pretty pointless up to now, yes there will sometimes be a requirement to display some bespoke static content in a Canvas App or Page Tab but really this isn't overly likely, and doesn't benefit from any of the advantages that we've come to expect from using Facebook. However, this starts to get interesting when we combine this with the new Facebook Open Graph Protocol.

By having your users connect to your site using Facebook Connect they are granting you permission to read and write to specific areas of Facebook on their behalf. Facebook uses OAuth to authenticate users and when your site invokes a request to get an authentication token for a specific user you also tell Facebook what you intend to do as this user. So for example a request in the following form would ask for permission to have you site read a user's email address and their news feed as well as their basic information. All full list of these permissions can be found at http://developers.facebook.com/docs/reference/api/permissions/.

https://www.facebook.com/dialog/oauth?
     client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream

Once your site is connected to a Facebook account you can then start to effectively use Facebook as this user within the confines of the permissions they have granted. This means that you can do clever things like post back to their news feed when they read an article on your site this is where you will start to make use of your app's namespace which was created right at the beginning of this process. You can find a tutorial on how to go about using the Open Graph Protocol at http://developers.facebook.com/docs/opengraph/tutorial/.

Tying this in with Drupal

Finally it is worth noting that a lot of the grunt work has already been done to get your Drupal site working with the Facebook Graph in the form of the Drupal for Facebook module. There isn't a stable release of this for Drupal 7 yet but the basics are there and it serves as very useful starting point if you want to develop an all singing all dancing integration with Facebook.

Comments

For reference on using the Drupal for Facebook module in Drupal 7 -- we use the latest revision (as of 12th March), that's revision 31e994, alongside the following patches to fix various issues (of various significance) that have not yet been committed or are not yet fully complete for all situations, but fit the cases we are currently facing on projects:

http://drupal.org/files/issues/fb-settings-error-1182190-no-prefix.patch (Fixes fb_settings() not always being defined, see: http://drupal.org/node/1182190 )
http://drupal.org/files/issues/fb-get-fbu-notices-1182218-no-prefix.patch (Fixes notices within fb_get_fbu function, see: http://drupal.org/node/1182218 )

The following few patches may not work for multilingual sites (please test and help out in the module's issue queue!):
http://drupal.org/files/issues/fb-default-locale-setting.patch (Fixes default locale settings on FB config form)
http://drupal.org/files/fb-1084946-path-prefixes-2.patch (Allow path prefixes to work in FB correctly, see: http://drupal.org/node/1084946#comment-4952146 )
http://drupal.org/files/issues/fb-1197924-language-negotiation.patch (Fixes PHP error about undefined constant LANGUAGE_NEGOTIATION_NONE, see: http://drupal.org/node/1197924#comment-4957700 )
... you may find these two patches clash, so this following patch incorporates the above two patches, rolled against commit b87c26 of the module: http://dl.dropbox.com/u/6359021/cm-fb-url-alters-patches.patch

http://drupal.org/files/issues/save-user-notice-1117214-4.patch (Fixes PHP notice on disconnecting account on saving profile.)
http://drupal.org/files/fb-1308330-logout-js.patch (Fixes Unable to logout without JS: http://drupal.org/node/1308330.)
http://drupal.org/files/fb-1308460-fb_cookie-notice.patch (Fixes notices in fb_canvas: http://drupal.org/node/1308460#comment-5111582 )
http://drupal.org/files/fb-signed-request.patch (Ensures signed request gets passed to Drupal / FB SDK. See http://drupal.org/node/1370778 )
http://drupal.org/files/fbsr-cookies-3.patch (Fixes infinite redirect caused by cookie name change. See http://drupal.org/node/1380534 )
http://drupal.org/files/fb-proxied-email-notice.patch (Fixes PHP notice on loading some users. See http://drupal.org/node/1440820 )

Sorry for the dump of links, but since the D7 version of the DFF module is rather 'fluid' in its stability, I figure someone might need to know these :-)

Comments on this article are now closed, if you want to give us feeback you can use our contact form instead.