« Your First Facebook Application »
Writing a Facebook Application
With the rise of social network applications such as Facebook over the last 3 - 5 years, it's become very attractive to be present in that space. Facebook applications are most often games that allow users to play against each other, or themselves in the case of solo-games, but there is no technology limit to this. The application could easily be an extension of your own application.
One of the many great things you get from using the Facebook platform, is an
enormous list of potential clients who already have user/login info
for your application! Couple this with the outsourcing of user login/management/authentication to another provider (Facebook), access to the Facebook messaging API, and it's easy to see the return on investment.
Getting Started
Assuming you have a Facebook account , you will need to install the Facebook developer application . Once you have the developer application installed, you should be directed to the developer home page, and here you can apply for an application key. When you apply for an application key, it asks for an application name, we'll call ours Hello World. Now that you have a Facebook application registered, you can retrieve your API key, and secret key from the installed applications page.
We will be building a .NET application using the .NET Facebook dev kit from Microsoft , but you can use any modern language. There are development libraries available for all of them, and the developers wiki is a great resource... here is the link.
Assuming you will be developing using your local machine, we need to configure a couple values in your Facebook application, which can be done on the application settings you can access from the applications page here.
- Set your Callback URL to your local instance url (e.g. http://localhost/); if you are using Visual Studio's built in server/debugging, then you will need to fix the port that it uses, and enter that as your Callback URL. If you are using a public server, then enter the URL for that server in this box.
- Set your Canvas Page URL to anything you like, this is the url that Facebook users will use to access your application.
- Set the application to iFrame - This allows you to have any content you choose, without being limited by the FBML (Facebook Markup Language) and it's slow rendering times.
- Set the application type to Website.
There are many more options there that you can play with , but these are the ones you need to get your application running.
After installing the Facebook Dev Kit from Microsoft, you will have the Facebook libraries located in the install location (C:\Program Files\Facebook Developer Toolkit). There will now be Facebook components available in Visual Studio, but we will do our example by hand.
The Code
Create a new web page project, and open Default.aspx to insert the following content:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HelloWorld.aspx.cs" Inherits="HelloWorld._default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>FaceBook Hello World</title>
</head>
<body>
<form id="form1" runat="server">
<h1>Ours</h1>
<asp:Repeater ID="friendList" runat="server">
<HeaderTemplate>
<table>
<tr>
<td><b>Name</b></td><td><b>Photo</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate><tr><td><%#DataBinder.Eval(Container.DataItem,"Name")%></td>
<td><img src='<%#DataBinder.Eval(Container.DataItem,"PictureUrl")%>'
/></td></tr></ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
Now open the code behind file Default.aspx.cs , and populate it with this
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.ObjectModel;
namespace HelloWorld
{
public partial class _default : System.Web.UI.Page
{
private Facebook.Components.FacebookService _facebookApi = new Facebook.Components.FacebookService();
protected void Page_Load(object sender, EventArgs e)
{
// ApplicationKey and Secret are acquired when you sign up for
_facebookApi.ApplicationKey = "[YOUR KEY]";
_facebookApi.Secret = "[YOUR SECRET KEY]";
_facebookApi.IsDesktopApplication = false;
string sessionKey = Session["Facebook_session_key"] as String;
string userId = Session["Facebook_userId"] as String;
// When the user uses the Facebook login page, the redirect back here will will have the auth_token in the query params
string authToken = Request.QueryString["auth_token"];
if (!String.IsNullOrEmpty(sessionKey))
{
_facebookApi.SessionKey = sessionKey;
}
else if (!String.IsNullOrEmpty(authToken))
{
_facebookApi.CreateSession(authToken);
Session["Facebook_session_key"] = _facebookApi.SessionKey;
}
else
{
Response.Redirect(@"http://www.Facebook.com/login.php?api_key=" + _facebookApi.ApplicationKey + @"&v=1.0");
}
if (!IsPostBack)
{
// Get our list of friends
Collection<Facebook.Entity.User> friends = _facebookApi.GetFriends();
//bind our repeater
friendList.DataSource = friends;
friendList.DataBind();
}
}
}
}
The basics here should be clear. You set your application and secret keys, and then your basically good to go! The Facebook.Components.FacebookService class provides access to the current user, and if they aren't logged in you can bounce them to the Facebook login screen.
To go live with your project, simply move the code to a public server, change your callback url in the application settings page within Facebook and select the 'add application to directory' check box on that same page.
Now that you have the user authenticated, and access to their friends list, the sky is the limit. We chose to store user keys in our database for customizations which we simply did using their Facebook ID.
In Conclusion
In practice I found the Facebook API to be very usable, and there were no unexpected hurdles to work around. We did have an occasional user authentication error, which may have been caused by having mutliple users logged in on the same machine, but this was an edge case.
In all, I wouldn't hesitate to recommend the platform as an extension of an existing business giving a low-cost, low-risk entry into the seemingly persistent world of Social Networking Applications. However, I would hesitate before putting serious development into a FBML application, or anything that specifically required Facebook, for the same reasons I wouldn't want to own a million dollar application that only ran in Friendster, Tribe.net or any of the other Social Networking sites that had their time in the sun.
9 Comments | 
Reader Comments (9)
The person registers there details and away they go.
How do i port this so it auto registers there facebook details within the database?
Basic Code for beginners but this code is first step toward your success good one thanks for share..
Thank you so much to share so nice article. And we also would like to sharing karen millen outlet.Karen millen has been committed to provide the elegant, sexy dresses and coats. karen millen outlet is your best choice to participate in party.
ugg bailey button triplet is really a legendary make of sheepskin boots. Apparently simple-minded cartoon popular in Asia and europe, however the shape of the world, and it is now a fad popular on the planet the wind.GG uses the greatest quality leathers and suedes, not to mention, the earth's finest Grade "A" Merino sheepskin. bailey button triplet ugg United kingdom uses just the greatest-grade sheepskin available. Twin-face sheepskin can be used in a number of our core items. A bit of twin-face sheepskin continues to be treated on the fleece side, and also the skin side, supplying the soft comfort ugg bailey button triplet boots Australia is known for.Boots adopted within world additionally to continents thwart I as aircraft pilots used these lower as high as lined air uggs generally referred to as Uggs Clearance. Uggs are applied consequently of slightly substantial understanding sheepskin referred to as 'twin faced' this specific energy solved on from both edges inner together with outer. This notable gear items trunk to exhale and wick falling in dried out ft.Comfortable altering design also enables variations of ugg bailey button triplet sale , inside a couple of years aboard the main worldwide magazines, so fur boots craze is sweeping the planet.
Refer the bcbg max azria dresses , tt is have lots styles,for example: Herve Leger bustier dress from your Herve Leger collections are inspired by previous designs and outdoors influences, because its designes without strap, using the bust top only,you don't need to make more adornments, the sexy already showing directly, that's why it's welcome by all women in each and every periods of favor.If you wish to show its fashion capabilities, to provide yourself any 72 alter, this Herve Leger Single Strap is a superb choice! Unique cloud styles, creating any noble sense of puzzle. Simple, good, Star Supporter, even when merely a idea drill side nail furnishings, Hevre leger is furthermore the popularity within the OL since easily setup the look within the increase regarding women.Women can't do with no beautiful dress, Herve Leger Dresses will assist you to show the wonder and have the time happiness. The sexy. This women at Herve Leger Sale looked very charming and sexy.
En ting at huske er at v?lge en klassisk stil Aviator Canada Goose Jakke , sandsynligvis i sort eller m?rk brun, for at sikre, at denne canada goose trillium publish er lige s? alsidig som det kan v?re. Valg af et Aviator jakke, der er blevet-overdesigned eller v?lge en i en lys farve vil begr?nse, hvad du kan b?re det mediterranean, og vil klart se ud som du f?lger en trend, der begr?nser dets holdbarhed og g?re det mere af en impuls k?b finish en investering i en vigtig brik til din garderobe.Motorcykel jakken har sin egen stil af canada goose danmark opg?relse, der adskiller sig meget i forhold til andre Canada Goose Jakker . Motorcykel jakke er af to typer, living room ene som er almindeligt ses p? banen hedder stykke og living room canada goose trillium anden 2011 Canada Goose Kids Parka kombineret af l?der buks og sko. Det Canada Goose Banff Parka Biker foretr?kker l?der motorcykel Canada Goose Outlet generelt designet have metalplettering sammen mediterranean dens tilbeh?r for vagt din krop.Males du v?lger at b?re din Aviator jakke i denne s?boy, vil det tilf?je et strejf af catwalken til ethvert outfit og sikre, 2011 Canada Goose Kids Parka at du er forberedt p? det kolde vejr og husk at du ikke beh?ver at bruge en formue p? Canada Goose Danmark living room seneste trend, og det vil holde search langt ud i slutningen af ??denne vinter.
At the same time, the investigation<h1>Coach Outlet</h1> that led the United States to <h1>Coach Outlet</h1>the bank, the Lebanese Canadian<h1>Chanel Handbags</h1> Bank, provides new insights <h1>Coach Outlet</h1>into the murky sources of<h1>Coach Outlet</h1> Hezbollah’s money. While law enforcement <h1>Chanel Bags</h1>agencies around the world <h1>Coach Factory Outlet</h1>have long believed that <h1>Coach Outlet Online</h1>Hezbollah is a passive beneficiary <h1>Coach Outlet</h1>of contributions from loyalists <h1>Coach Outlet</h1>abroad involved in drug trafficking <h1>Coach Outlet Online</h1>and a grab bag of other<h1>Louis Vuitton Bags</h1> criminal enterprises, intelligence <h1>Coach Factory Outlet</h1>from several countries points <h1>Louis Vuitton Bags</h1>to the direct involvement of <h1>Coach Factory Store</h1>high-level Hezbollah officials <h1>Coach Outlet</h1>in the South American cocaine<h1>Coach Factory Outlet</h1> trade.One agent involved in the<h1>Coach Outlet Store</h1> investigation compared Hezbollah <h1>Coach Factory Outlet</h1>to the Mafia, saying, “They <h1>Coach Outlet</h1>operate like the Gambinos on <h1>Chanel Bags</h1>steroids.”On Tuesday, federal <h1>Chanel Bags</h1>prosecutors in Virginia announced <h1>Louis Vuitton</h1>the indictment of the man <h1>Coach Factory Store</h1>at the center of the Lebanese<h1>Chanel Handbags</h1> Canadian Bank case, charging <h1>Coach Outlet</h1>that he had trafficked drugs <h1>Chanel Bags</h1>and laundered money not only <h1>Louis Vuitton</h1>for Colombian cartels, but also for<h1>Coach Factory Online</h1> the murderous Mexican gang <h1>Coach Outlet</h1>Los Zetas.The revelations about <h1>Chanel Bags</h1>Hezbollah and the Lebanese Canadian<h1>Coach Outlet</h1> Bank reflect the changing <h1>Louis Vuitton Bags</h1>political and military dynamics<h1>Coach Factory Outlet</h1> of Lebanon and the Middle East. <h1>Chanel Bags</h1>American intelligence analysts believe<h1>Coach Factory</h1> that for years Hezbollah