Looks like a brand new SDK to play with for facebook. While they have other sdks for python, php, javascript, etc, this is their first official C# SDK. Previously the primary SDK that was available was the Facebook Developers Toolkit, which hasn’t been updated in some time (and hasn’t been updated to support the open graph api as far as I can tell).

You can grab the code from github at http://github.com/facebook/csharp-sdk.


One interesting thing to note, the current alpha build doesn’t provide a method to get an access token for the current user. The access token allows you to perform authorized requests on behalf of a user, and can be obtained via a redirect from Facebook, after the user has authenticated themselves. Without that token you can only access public data for a given user. The code is fairly simple in that case (this is taken directly from the console sample application on github):

string token = null;
Facebook.FacebookAPI api = new Facebook.FacebookAPI(token);
JSONObject me = api.Get("/4");
Console.WriteLine(me.Dictionary["name"].String);

Essentially, when you pass in a null token you can only access a user’s public data by passing in the user’s Facebook ID. 4 is apparently Mark Zuckerberg’s FB ID, makes me wonder who the first three were.

In any case, the lack of a way to obtain a user’s access_token makes sense since the SDK is meant to be used in multiple types of apps. This sounds like an opportunity to write some code, YAY! I’ve actually already detailed some of this previously.

Retrieving the access_token is pretty straightforward, since it’s available in a cookie that the user gets after authenticating. The name of this cookie is fbs_[AppID], where [AppID] is whatever application id facebook assigned to you. I’ve created a sample ASP.NET MVC 2 application that demonstrates this. It uses a simple wrapper class (FacebookConnect) to pull out the access_token for the currently logged in user, available as a property. Once you have the access_token you can then make authorized requests for the user:

FacebookConnect fbConnect = new FacebookConnect();
if (fbConnect.IsConnected)
{
    Facebook.FacebookAPI api = new Facebook.FacebookAPI(fbConnect.AccessToken);
    JSONObject me = api.Get("/" + fbConnect.UserID);
    name = me.Dictionary["name"].String;
}

I did run into an issue when setting the access_token on the FacebookAPI object. When the url is constructed it passes the Dictionary to an EncodeDictionary method (line 205 in FacebookAPI.cs), which encodes the key-value pairs using the HttpUtility.UrlEncode method. This caused problems when making calls to the graph, specifically an OAuthException : Invalid OAuth access token. Calls typically look like the following url (note that I’ve personally tweaked these urls so they wont actually work now, I’ve listed them just to demonstrate the issue):

https://graph.facebook.com/727440495?access_token=132568260112276|2.Z_FaL6rzoNMt2D3jfnEaWP__.3600.1279345500-727440495|77SIciuK333BmipPho0EIdJ8bwl.

or, after encoding

https://graph.facebook.com/727440495?access_token=132568260112276%7C2.Z_FaL6rzoNMt2D3jfnEaWP__.3600.1279345500-727440495%7C77SIciuK333BmipPho0EIdJ8bwl.

After the call to the EncodeDictionary method the url looks like this:

https://graph.facebook.com/727440495?access_token=132568260112276%257C2.Z_FaL6rzoNMt2D3jfnEaWP__.3600.1279345500-727440495%257C77SIciuK333BmipPho0EIdJ8bwl.

The pipe character in the original url is being double encoded to %257C. So when you try calling this url you get this returned:

{
   "error": {
      "type": "OAuthException",
      "message": "Invalid OAuth access token."
   }
}

The web exception thrown in the project is slightly more vague “The remote server returned an error: (400) Bad Request.”

For the time being I’ve just modified the FacebookAPI.cs class in the project to check for “access_token”, and skip encoding if an access token is passed in, though I guess another solution could be to just make sure you UrlDecode any access token you pass into the constructor:

if (kvp.Key.ToLower() == "access_token")
{
	sb.Append(kvp.Value);
}
else
{
	sb.Append(HttpUtility.UrlEncode(kvp.Value));
}

A couple of other things to note:
1. This is a MVC 2 web app, in Visual Studio 2010
2. You need to set your app id in the web.config file
3. I’ve also included two classes that I’ve used on previous projects to make calls to the open graph api, DEMOOpenGraph and DEMOFacebookUser. These use a DataContract and the DataContractJsonSerializer class to populate a strongly typed object. Below is some sample code showing it’s use:

OpenGraph graph = new OpenGraph();
facebookUser = graph.GetCurrentUser(FacebookConnect.AccessToken);
string firstName = facebookUser.First_Name;

This is obviously less flexible than the dictionary that the official API uses
4. You should also set the AppID value in web.config to your own value, I’ve left my own sample appid in there.

Download the code here

UPDATE
I’ve reworked the sample and provided a description for some of the new functionality here

http://onishimura.com/2010/08/11/facebook-c-and-asp-net-mvc-code-samples-for-friends-list-activities-list-and-wall-posts/