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.
UPDATE
I’ve reworked the sample and provided a description for some of the new functionality here

Amiable brief and this fill someone in on helped me alot in my college assignement. Gratefulness you on your information.
Would you happen to know how to handle getting friends and activities for the currently logged in user? I’ve implemented your DEMOFacebookUser class, but it’s failing when I add the following:
[System.Runtime.Serialization.DataMember(Name = "activities")]
public Array Activities { get; set; }
Because of the way the JSON is setup I’m not sure how to Serialize it, some applies to the friends response from OpenGraph.
Any help would be greatly appreciated.
Thanks,
Mike Murray
Sure, I’ve been meaning to go over that in another post so I’ll write it up and post more code tonight/this weekend. Basically it boils down to something like the following:
1. Make sure you have your perms on login to request friends/activities
2. Add classes to map the json objects properly…the friends list is actually in a ‘data’ element if you call https://graph.facebook.com/me/friends?access_token=... so you need to make sure your objects reflect that
3. Modify the Call method in demoopengraph to use JavaScriptSerializer. For whatever reason the DataContractSerializer was throwing fits when parsing the friends list, I think JavascriptSerializer is more
lightweight and flexible anyways.
You can also use the official facebook sdk with a call like :
JSONObject friends = api.Get(“/me/friends”);
Will go over this all in more detail soon.
A bit later than I anticipated but I’ve put up a post describing this and some sample code to go along with it at http://onishimura.com/2010/08/11/facebook-c-and-asp-net-mvc-code-samples-for-friends-list-activities-list-and-wall-posts/
Thanks again! You rock!
hi
i m using ur library,and i want to delete post.but it is not working.
i got an error
The remote server returned an error: (400) Bad Request.
please reply..its urgent.
HI ALL,
Please help me for this error , i downloaded above the project and while building this project i am getting error in web.config file.
Please anyone help me for this error
Error 1 It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS. D:\FLIBBIX\MVC\FacebookSampleMVC2Appv2\FacebookSampleMVC2App\FacebookSampleMVC2App\Web.config 28
Awaiting your reply
Puneeth. G
Great post! Thanks for putting this together. My app isn’t an MVC app but I was really just interested in getting the logic you have for getting the access token. The code that retrieves the cookie always returns null for me and I’m not sure why the cookie isn’t there. I have cookie set to true in my FB.init script. Anything else that I can check to see why the cookie isn’t available?
How do I use it for asp.net 3.5 version.
Is this a “better” FaceBook SDK for C#? http://facebooksdk.codeplex.com Curious on what you think would be better to use for a fb website using MVC. Thanks.
For almost 3 days I have been unable to return gifts, send gifts, ask for help from neighbors etc. etc. I contacted Playdom, as instructed and their post today was the problem was resolved last night. WRONG. I have buildings awaiting help from my neighbors and cannot send requests for help. My granddaughter is having the same problem and she, too, has buildings needing neighbor-help which will expire soon. Please make sure these buildings do not expire before Playdom gets the problem fixed for everyone.
I enjoy Facebook and play every day but am becoming quite frustrated over this mess. I have accomplished helping out friends needing Crystals but that is all I have been successful in sending.
Thank you,
Charlotte Jones