Using the Facebook Developer's Toolkit To Build A Facebook Browser in C#
In this article I'm going to explain how I used the Facebook Developer's Toolkit 3.0 to access the Facebook API when building the web browser known as Highway.  The Facebook Developer's Toolkit is a .NET wrapper around the Facebook API.  (The actual Facebook API was written in PHP).

Background

The Highway program itself is a Windows Forms application. The idea behind Highway was to create a web browser that would be seamlessly integrated to Facebook in order to allow users to access some of the Facebook functionality without actually having to visit the Facebook web site. 

Highway is written in C#.  As mentioned earlier, it's a Windows Forms application.  However, the Facebook Developer's Toolkit can be used from an ASP.NET application as well.  With the exception of the login functionality, all of the code in this article can be used either way.  (The login functionality I'll be illustrating is for Facebook Desktop applications, which is loosely analagous to Windows Forms applications.) You can read more about Highway and download it if you wish from my web site. Highway is offered free of charge.  The Facebook Developer's Toolkit 3.0 can be downloaded from the Codeplex web site.  Links to both sites are provided at the end of this article.

This article assumes that the reader has a good grasp of the C# programming language, as well as how to reference third party assemblies.  Familiarity with Facebook and its "lingo" is also assumed.  I won't be discussing how the web browser control works, nor will I be discussing design decisions taken when creating Highway.  The article is meant solely as a tutorial on how to implement specific Facebook functionality using C# and the Facebook Developer's Toolkit 3.0.

 Some of the Facebook related features of Highway that I'll be explaining and providing code for include:
  • Posting a message on the user's wall (also known as updating your status)
  • Posting a link to the current web page (the page currently loaded in the browser control) on the user's wall
  • Accessing the user's Facebook friends' names and photos
  • Accessing the user's Live Feed

Preparation

Before we get into the actual code, there are a couple of things we need to address.  First, we need to install and reference the Facebook Developer's Toolkit assemblies.  There are several of them, but we're only concerned with three; Facebook.dll, Facebook.Web.dll, and Facebook.Winforms.dll.  A link to the Facebook Developer's Toolkit download is included at the end of this article.) You will need to reference these three assemblies in your project.

The second thing we need to address is to register your application with Facebook.  In order for your application to be permitted access to the Facebook API, you will need to register it with Facebook.  You can do this by going to the Developer App in Facebook, at http://www.facebook.com/developers .  For a desktop application like Highway, you will need to indicate that the application is a desktop app, and you will need to supply two different callback url's, one for login success (for example http://[yourdomain]/FB/login_success.aspx) and one for failure (for example http://[yourdomain]/FB/login_failure.aspx). You will need to host these url's on a web server.  Once your application is registered, Facebook issues an API Key, which you can find right there in the application's profile.  The API Key identifies your application to Facebook, and will need to be provided each time you call an API function. There is a bit more to the registration process, but those are the crucial pieces.  Facebook's web site has pretty good documentation at the above url to help you with the rest if necessary.

Here We Go

The first thing we need to do is log the user in to Facebook.  While it is possible to store user login information and log the user in progrmatically (we allow users that option in Highway), it is far simpler to leverage Facebook's own login screen.  The first code block below shows the login_success.aspx.cs file which, as mentioned earlier, is where Facebook redirects your user upon successful login.  It receives a "session" parameter with the request and assigns its value to a label control prior to being sent back to the user.  Here's the code:

 The login_failure.aspx file will get called if login fails.  You can handle that in any way that makes sense for your application.

I've created a static class called FBSession to hold the user's session info after he's been logged in:

 Next, we'll write the code that calls the Facebook login functionality and retrieves the response from login_success.aspx.  All we need to do is navigate to Facebook's web site.  After navigation, in the browser control's Document_Complete, event we check for the existence of the labelSess control, then parse its value.  The format of the value is a JSON string.  Note that the FBSession class is immediately populated:

Now that we've got the user logged in and we've got his user id and other session info in hand, we can proceed to the fun stuff!

Posting a message on the user's wall (also known as updating your status)

A Facebook user can post messages to his or her wall for all their friends to see.  This action is also know as updating your status.  This is probably the simplest of all the functions we'll be looking at.  Take a look at the code below.  An explanation follows.

The ConnectToFacebook() function is pretty straightforward.  The first method called is FBConnect(), which returns a valid Facebook session. Within the FBConnect() method we begin to make use of the FBSession object we populated upon login to instantiate a DesktopSession object, passing in the API Key for the application, along with a boolean value which represents whether or not our application uses WPF.  For our purposes we can pass false for this parameter.  Note that there are a couple of session types available that correspond to the different Facebook application types. Although we are using the DesktopSession, an FBMLCanvasSession object is available as well for web applications.

Next, we instantiate an Api object by passing the DesktopSession to its constructor.  We then assign our stored Session_key and Secret values to the Api's Session.SessionKey and Session.Secret attributes, call the Facebook.Rest.Api.Users.GetLoggedInUser() method to obtain the user's user id, and we return it to the calling function.

Back in ConnectToFacebook() we now call postStatus(), passing in the user id returned by FBConnect(). Within postStatus(), we call Users.GetInfo() and pass the user id.  Users.GetInfo() will return a Facebook.Schema.user object.  Finally, we make a call to Facebook.Rest.Api.Stream.Publish(), passing a string containing the message that the user wants to post to his wall.

Posting a link to the current web page (the page currently loaded in the browser control) on the user's wall

Next up, we're going to allow the logged in user to post a link to his own wall.  I'm going to make a call to FBConnect() again, but for the sake of time and space (Einstein would be proud), I won't include the listing for that function again.  In Highway, I use this function to allow the user to post a link to the web page he is currently looking at, referencing the webBrowser control's Url property.  However, you can actually pass in any Url depending on the need of your application and it'll work just as well.  Here's the code:

Pretty simple stuff.  We're just calling FBConnect() to get the logged in user's user id, then within postLink() we call Users.GetInfo() to grab the Facebook.Schema.user object.  Then we call the Facebook.Rest.Api.Links.Post() method, passing in the user id as an Int64, the url of the site we want to post the link for, and the user's comments, if any.  Facebook actually examines the document returned by the Url and creates its own summary of the site for display based on the Title tag and the Description meta tag, so there's no need to worry over that.

Accessing the user's Facebook friends' names and photos

Now we'll get a look at how to access information on the logged in user's friends.  For simplicity's sake, I'll get them, access a few properties of each friend, and display them in an html table.

If you want, you can even obtain the user id for each friend, and if they authorize your application, in turn use it to access their friends.

Accessing the user's Live Feed

Accessing the user's Live Feed actually involves passing a List object containing the id's of the user and all of his friends, to the Facebook.Rest.Api.Stream.Get() object.  The Stream.Get() object takes three other parameters; DateTime beginDate, DateTime endDate, and int maxCount, which allow you to control the date range and maximum number of items returned.

The code shown in the GenNews() function below illustrates how to iterate through the collection of post.stream_post objects contained in the stream_data object that the Stream.Get() returns.  It also shows you how to determine who the "actor" for each post was.  The actor could be the logged in user himself, or any of the friends included in the list that was passed to Stream.Get().  The stream_post object has quite a few properties and methods which will allow you to duplicate Facebook's Live Feed page (or improve upon it).  Those properties can be found in the Facebook Developer's Toolkit documentation, or simply by using Intellisense.

We make use of the stream_data object in the GenNews() method.  The stream_data objects contains a posts.stream_post collection.  Each item in the collection represents a post in the user's Live Feed.  As we iterate through the collection, the first thing we do for each post is check the user id of the user who made the post (contained in the post.actor_id property) and compare it to the user id of the logged in user.  If they are equal, then we've already got the actor's info, and we assign it to the poster user object.  If not, we locate the actor id in the logged in user's friends list and assign his user object to the poster user object.

I hope that with this article I've been able to shed some light for you on the subject of using the Facebook Developer's Toolkit to interact with Facebook from a .NET application.

For more information about Highway and to download the program, visit our web site at http://www.murrayhilltech.com/highway.aspx .

To read more about the Facebook Developer's Toolkit and for instructions on downloading and installing it, visit the Codeplex web site at http://facebooktoolkit.codeplex.com .

Dave Verschleiser
Murray Hill Technologies


DateComment
No records to display.

Enter your comments here:


Enter your name:   
 
 


===============================================================================================================