The following C# code extract has been written to demonstrate how to use the Facebook Development Toolkit from Clarity Consulting.
It’s assumed that your application has been registered with Facebook and you have the necessary keys. This is done by adding the developer application to your Facebook profile and registering your application. If you need more information on how to do this I would recommend reading the following Facebook Getting Started Guide.
Written below is a code extract that updates the status of the person who has logged into the application. The process of logging into Facebook is handled by the Session.Login() method by presenting a modal dialog box to the user.
Due to the threading model of the API it is important to use the STAThread attribute when creating the Main application method.
[STAThread]
static void Main(string[] args)
{.....
If this isn’t done an “ActiveX control ’8856f961-340a-11d0-a96b-00c04fd705a2′ cannot be instantiated because the current thread is not in a single-threaded apartment” exception will be thrown.
Before updating the user’s status it’s important to ask their permission by adding an ExtendedPermissions.status_update enum to the application session. This will display a confirmation dialog to the user after they have logged into the application.
List<Enums.ExtendedPermissions> Permissions = new List<Enums.ExtendedPermissions>();
Permissions.Add(Enums.ExtendedPermissions.status_update);
The following is where the application and application secret keys are added to the session. It’s important to read all of the recommendations on how to use the app keys as they can be used outside of your application if obtained through App.Config files.
All development recommendations can be found in the Facebook Developers Wiki.
Facebook.Session.DesktopSession d = new Facebook.Session.DesktopSession("myAppKeyFrom Facebook", "mySessionSecretFromFacebook", "mySessionKey", false, Permissions);
//display application login to user
d.Login();
After the session has been created it is then possible to create an instance of the Facebook Rest API. This is where all of the supported methods can be found that control the status of the logged in Facebook profile. Within the context of this example it is the Status.Set method that is being called.
Facebook.Rest.Api Api = new Facebook.Rest.Api(d);
Below is where the magic happens and the user’s status is updated. I’ve decided to go for the traditional ‘hello world’ example to add a bit of weight to my geekhood.
Api.Status.Set("has created a typical hello world status!");
Finally the user is logged out of the system.
d.Logout();
I hope you find this post useful as I know there aren’t very many examples out there at the moment. Feel free to add your examples to this page as it’s always a pleasure to share your views with the rest of the development community.
About James McLeod, Managing Director at Narvi Digital Media, Brighton



Hi,
Last time I took a look on the Facebook API they wanted me as a developer to provide a key and a secret that I cannot distribute for the requests. This requirement does prevent me from using the Facebook API in my client side application – does Facebook not need that key any more, so that it’s possible to write and distribute desktop applications without disclosing a developer/application key?
Regards,
Sven
By: Sven on November 27, 2009
at 5:15 am
Hi Sven,
Thanks for the comment. Basically, the Application Secret isn’t required when posting to Facebook using the MS API. If you follow the code example below you’ll notice the DesktopSession object picks up the session information from the Facebook response. These are the keys that get passed with the subsequent API requests. You do need to pass your AppKey though.
string AppKey = "AppKey";
bool WPF = false;
DesktopSession d = new DesktopSession(AppKey, WPF);
d.Login();
Console.WriteLine(d.SessionKey);
Console.WriteLine(d.SessionSecret);
Api Api = new Api(d);
Api.Status.Set("My status update....");
d.Logout();
I hope this helps,
James.
By: followmcleod on November 27, 2009
at 9:37 pm