Publish Your Application Stories to Facebook
My article "Use Facebook Connect to Bring Your Application to Millions of Users" described how you can leverage the huge Facebook user base to make your application easier to use through a simplified authentication process. In this article, we'll focus on publishing stories from your application to Facebook. The satisfied users of your application will love to publish the stories (it's an easy way to brag about their achievements in their social network), and the links in the stories might bring their friends to your website.
Publishing stories on Facebook is simple; all it takes is running the Create Feed Template wizard from the Facebook developer site and adding a few lines of JavaScript in your application. Let's get started.
Create a Facebook Feed Template
Before you can start publishing stories from your application to Facebook newsfeeds, you have to create a story templatea short text that will appear on the user's Facebook wall and in the newsfeeds of her friends when she decides to publish your story to Facebook.
The story templates can include a one-line story (heading) and a short story (text). At minimum, you must define the title of the short story, otherwise your stories will not be posted to Facebook newsfeeds. You can use FBML and HTML in the templates as well as additional tokens marked with {*token-name*} that you can pass from your application to Facebook.
Some of the tokens are predefined. For example, the {*actor*} token is mandatory and represents the Facebook user's name. You can include other tokens in the JavaScript Facebook Connect call, and they'll be inserted into the proper places in the template.
To create a feed template, follow these simple steps:
- From the list of your Facebook applications, select the application you want to modify.
- Click the Create Feed Template link in the right-hand sidebar.
- Select your Facebook application in the Feed Template Console.
- Enter the one-line story template. You can also enter JSON-encoded token values and preview the results of your template (see Figure 1). We'll use a very simple template that includes just the user's name and a link to the message she has published:
- When you're finished with the one-line story template, click the Next button to move to the short story template. You have to enter the short story template even if it's identical to the one-line story template; otherwise, your stories will not be published from Facebook Connect. We'll use the same text in the short story title as in the one-line story, and for now we'll leave the short story body empty (see Figure 2). When you're finished, move forward by clicking the Next button.
- You should enter an optional Action link that Facebook users can follow to reach your application. Enter the main URL of your application in the Action Link URL field, and type some invitation text in the Action Link Text field (see Figure 3). Move to the next step by clicking the Next or Skip button.
- Review your template and click the Register Template Bundle button when you're finished. A pop-up dialog appears, displaying your new Template Bundle ID, which you'll need in your Facebook Connect calls (see Figure 4).
{*actor*} <a href="{*href*}">wrote a new message</a>
If you forget to copy the Template Bundle ID from the pop-up dialog, or you lose the number, you can list all the template bundles registered by your Facebook application in the Registered Templates Console tab. Figure 5 shows the one-line story template registered by my InformIT Chat Application.