Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / WPF

Getting Started with Facebook Desktop (Client) Applications, C#, WPF / XAML and JSON

Rate me:
Please Sign up or sign in to vote.
4.92/5 (28 votes)
21 Jan 2009CPOL15 min read 320.3K   11.7K   139  
A take on getting started with the Facebook API and WPF
using System;
using System.Collections.Generic;

namespace Facebook
{
    /// <summary>
    /// Class containing all you need to login in and set up a session is in this class
    /// </summary>
    internal class Login
    {
        /// <summary>
        /// Create an Auth Token, login to facebook and set up a session for further API calls
        ///  http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application
        /// </summary>
        internal static Facebook.auth.getSession LoginAndGetSession()
        {
            string facebookAuthToken = CreateAuthToken(); // Step 1: Create Auth token
            LoginToFacebook(facebookAuthToken);           // Step 2: Login to facebook
            return GetSession(facebookAuthToken);         // Step 3: Return a session 
        }

        /// <summary>
        /// Create an Auth Token for facebook
        /// </summary>
        private static string CreateAuthToken()
        {
            // Create a token to use for the facebook session
            var parameters = new SortedDictionary<string, string>();  // Facebook parameter list (key value pair) that (needs to be sorted in order to get the MD5 hash correct - see Facebook.Security
            parameters.Add("method", "facebook.auth.createToken"); // http://wiki.developers.facebook.com/index.php/Auth.createToken
            string jsonResult = Facebook.Web.SendCallToFacebook(parameters);
            return JSON.Helper.StripSingleResult(jsonResult); // Return the Auth Token
        }

        /// <summary> 
        /// Login to facebook - needs to be done via the facebook web front end  
        /// </summary>
        private static void LoginToFacebook(string facebookAuthToken)
        {
            // Call the web form to do the facebook login - I couldnt find an easy work-around for a silent login :(
            var facebookLoginForm = new Facebook.Windows.Login(Facebook.Login.GetFacebookLoginPage(facebookAuthToken));
            facebookLoginForm.ShowDialog();
            if (!facebookLoginForm.AuthenticatedSuccessfully) throw new Exception("Error: facebook login failure");  // If not logged in, we can go no further
        }

        /// <summary>
        /// Create the facebook API session - http://wiki.developers.facebook.com/index.php/Auth.getSession
        /// </summary>
        private static Facebook.auth.getSession GetSession(string facebookAuthToken)
        {
            var parameters = new SortedDictionary<string, string>();  // Facebook parameter list (key value pair) that (needs to be sorted in order to get the MD5 hash correct - see Facebook.Security
            parameters.Add("method", "facebook.auth.getSession"); // http://wiki.developers.facebook.com/index.php/Auth.getSession
            parameters.Add("auth_token", facebookAuthToken);
            parameters.Add("generate_session_secret", "false");

            string jsonResult = Facebook.Web.SendCallToFacebook(parameters);
            return (JSON.Helper.Deserialize<Facebook.auth.getSession>(jsonResult));
        }

        /// <summary>
        /// Generate a url for login to facebook - this needs to be done against the facebook front end (for security reasons they say).
        /// </summary>
        /// <param name="auth_token"></param>
        /// <returns></returns>
        private static string GetFacebookLoginPage(string auth_token)
        {
            return String.Format(Facebook.Properties.Settings.Default.LoginUrl, Facebook.Properties.Settings.Default.ApplicationKey, auth_token, Facebook.Properties.Settings.Default.ApiVersion);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
You can read more about me here:

http://uk.linkedin.com/in/murrayfoxcroft

Comments and Discussions