This project is read-only.

Some additional methods for FacebookAPI.cs

Jul 21, 2008 at 9:25 PM
Hi there,

For my desktop application, I've started to write some additional methods which are not included yet in the FacebookAPI class and may be interessting for other developers.

First at all, I've added a ConnectToFacebookSilent class. The one with an opening browser is ugly and very unconfortable. There is no chance to write good console oder service applications. You have all the time to treat with STA and threading failures.
The code bellow is not really perfect and so on but it can be a foundation for a next method to interate in your framework.

        public bool ConnectToFacebookSilent(string email, string password)
            if (!IsSessionActive() && IsDesktopApplication)

                HttpWebRequest reqLoginPage = (HttpWebRequest)WebRequest.Create(LoginUrl);
                reqLoginPage.UserAgent = Resources.UserAgent;
                reqLoginPage.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)reqLoginPage.GetResponse();

                HttpWebRequest reqLogin = (HttpWebRequest) WebRequest.Create(Resources.FacebookLoginFormUrl);
                reqLogin.UserAgent = Resources.UserAgent;
                reqLogin.Method = "POST";
                reqLogin.ContentType = "application/x-www-form-urlencoded";
                reqLogin.CookieContainer = new CookieContainer();
                reqLogin.CookieContainer.SetCookies(new Uri(Resources.FacebookLoginFormUrl), response.Headers["Set-Cookie"]);

                byte[] parameterString = Encoding.ASCII.GetBytes(String.Format("version=1.0&auth_token={0}&api_key={1}&email={2}&pass={3}&save_login=&charset_test={4}",
                reqLogin.ContentLength = parameterString.Length;

                using (Stream buffer = reqLogin.GetRequestStream())
                    buffer.Write(parameterString, 0, parameterString.Length);

                WebResponse webResponse = reqLogin.GetResponse();

                if (webResponse.ResponseUri.AbsoluteUri.Contains("desktopapp.php"))
                    return true;

            return false;

Resources.CharsetTest = €,´,€,´,水,Д,Є
FacebookLoginFormUrl =
UserAgent = Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)

Then two more methodes I implemented: (implemented as extension methods, should be rewritten if you add this in your FacebookAPI)

        public static List<User> GetOnlineFriends(this FacebookAPI facebookAPI)
            Dictionary<string, string> parameterList = new Dictionary<string, string>(3);
            parameterList.Add("method", "facebook.fql.query");
                              "SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=",
                              ") AND 'active' IN online_presence"));

            string resultXml = facebookAPI.ExecuteApiCallString(parameterList, true);

            MatchCollection matches = Regex.Matches(resultXml, "<uid>([0-9]+)</uid>");

            if (matches.Count == 0)
                return new List<User>();

            List<string> uidList = new List<string>();
            foreach (Match uid in matches)

            Collection<string> uids = new Collection<string>(uidList);
            Collection<User> users = facebookAPI.GetUserInfo(uids);

            return users.ToList();

        public static bool IsConnected(this FacebookAPI facebookAPI)
            return facebookAPI.UserId != null;

So that's it. May you need it useful, so it would be glad to add it to your project.

Best regards
Jul 22, 2008 at 5:37 PM
I think you might have this project confused with the Facebook Developer Toolkit.