Console Application

Jul 9, 2009 at 6:06 AM
Edited Jul 9, 2009 at 8:15 AM


I'm trying to create a demo console application using Facebook Toolkit. Can you please tell me what is the best method to create such an application? I was trying the code above but it seems it's not working, because it throws the following exception: "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.":

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using facebook.Components;

namespace FacebookDemo
    class Program
        static void Main(string[] args)
            FacebookService fb_service = new FacebookService();
            fb_service.ApplicationKey = //the application key goes here
            fb_service.Secret = // the secret goes here;


Thank you in advance for your help.

PS: One more thing. Do you know if using the Facebook Developer Toolkit, will I be able to search for public posts, statuses and photos?


Jul 9, 2009 at 1:22 PM

Well, you can't do it really.  The problem is that you need to have a FacebookSession established that provides the context of the user you will interact with Facebook on behalf of.  When you are using FacebookService this is really a WinForm component.  When you call ConnectToFacebook it is showing a webbrowser control that allows a login.  This is the error you are hitting. 

If you are just trying to always interact as yourself.  You could setup a winform first do the login, and allow the session stay alive.  You can then capture the generated SessionKey, and session secret to use in your console app.  Yes, this is painful, but you can't really get around the fact that you need to authenticate and you can't do it programmatically only.

Yes you can read the users stream, get their status and photos.

Jul 9, 2009 at 1:38 PM

Thanks for yor early reply.

I managed to connect to Facebook and querying some information like friends, photos, etc. but only those related to my account. My question is how can I search over all Facebook network for a search term? I mean I want to get all the results related to my search term the same way as I get results when I'm using the search functionallity on Facebook's home page.




Jul 9, 2009 at 2:08 PM

There's no such call in the Facebook API, and to be honest, I doubt there ever will be. Facebook tends to be pretty limited in the data they make available, for performance reasons and to prevent people from doing bad things with their data (spamming users, etc.). I would think that allowing developers to programmatically do arbitrary searches as often as they want would create some performance issues, so I'm not sure I see that ever coming to fruition. Feel free to request it on the Facebook bug tracker, though:

Dec 30, 2009 at 10:24 AM


My question is to "ionutv". Did u manage to authenticate Facebook from a Console Application if yes how?

I am trying to do the same but could not get much help.

Thanks in advance

Dec 30, 2009 at 2:47 PM

Use the RestSession:



                string APIKey = ConfigurationManager.AppSettings["API_Key"];
                string APISecret = ConfigurationManager.AppSettings["API_Secret"];

                Facebook.Session.ConnectSession connectsession = new Facebook.Session.ConnectSession(APIKey, APISecret);
                Facebook.Rest.Api api = new Facebook.Rest.Api(connectsession);

If you call API methods that require a user session, you will need to obtain the offline_access extended permission and save the session key to use in those method calls.  But not everything requires a session key.


Jan 27, 2010 at 8:02 PM

I am unable to login to facebook or to maintain session

Facebook.Session.ConnectSession connectsession = new Facebook.Session.ConnectSession(APIKey, APISecret);
            Facebook.Rest.Api api = new Facebook.Rest.Api(connectsession);

I am getting this error

A session key is required for calling this method.