I live in Omaha.

How good is your Memory? Part I

by SwervinErv on 01.28.2010

A couple of months ago, The Silverlight Blog announced the Facebook SDK for Silverlight. I have been wanting to get a post up about using this SDK, but it has taken a while. Anyway, this is part one in what will be a three part series of a basic overview of using a little bit of what the Facebook SDK has available.

The Premise

Gathering profile and friend list information is made quite simple in the SDK, so I thought that an easy demo application would be an implementation of the childhood game Memory. However there will be a slight twist. Instead of using the same picture to match, the game will randomly select two profile pictures from your friends and use those for you to determine the match. I will post a link to a demo of the application once there's a full working version.

Making the connection 

The first step in getting this game started was being able to get the data from Facebook, and this is done using Facebook Connect. The SDK makes this very simple. Using the Facebook.Session.BrowserSession object, you simply create a new instance of a browser session with your Facebook application key, add a login completed event handler, and call BrowserSession.Login. This will open up a popup window with the Facebook connect login.

BrowserSession _browserSession;
_browserSession = new BrowserSession(APPLICATION_KEY);
_browserSession.LoginCompleted += LoginCompleted;
_browserSession.Login();

Getting the Information

Now that the user had logged in to their Facebook account, we can go ahead and get the user id of the logged in user. First we create a new instance of the Facebook.Rest.Api class and pass in the new browser session that was just created. The method call used is Users.GetLoggedInUserAsync and we will pass in a callback for when the operation is complete and any sate we wish to pass along in an object. In this case we will just pass null.

// we use a private member variable so that it can be re-used
private Api _facebookApi;

// create the new api instance and call the GetLoggedInUserAsync method
_facebookApi = new Api(_browserSession);
_facebookApi.Users.GetLoggedInUserAsync(GetLoggedInUserCompleted, null);

Next we need to handle the callback from the GetLoggedInUserAsync call. The callback method takes in a long parameter for the Facebook user id, an object for any state that you want to pass along, and any FacebookExceptions that may have occurred. Once the user id has been returned, we will make another call to the Facebook.Rest.Api class to get the user's information. This method is Users.GetInfoAsync where we will pass in the returned user id and a callback method. We will again use null for the state parameter.

void GetLoggedInUserCompleted(long userId, object state, FacebookException e)
{
    // check that no errors have occurred
    if (e == null)
    {
        _facebookApi.Users.GetInfoAsync(userId, GetUserInfoCompleted, null);
    }
    else
    {
        // if any errors occurred, invoke a call back to the UI thread and show the error message
        Dispatcher.BeginInvoke(() => MessageBox.Show("Error getting logged in user:\n\n" + e.InnerException.Message));
    }
}

Now that we have the current users information, we can get the information we want to pull from their profile. For this demo we will just use their name and a small version of their current profile picture. We will also set a _currentUser object that stores the user's information that we will use later in the application

void GetUserInfoCompleted(IList<user> userDetails, object state, FacebookException e)
{
    if (e == null)
    {
        Dispatcher.BeginInvoke(() =>
        {
            // set our user object
            this._currentUser = userDetails[0];

            // set user information data context
            this.ucUserInformation.DataContext = _currentUser.current_location;
        });
    }
    else
    {
        // if any errors occurred, invoke a call back to the UI thread and show the error message
        Dispatcher.BeginInvoke(() => MessageBox.Show("Error getting user info:\n\n" + e.Message));
    }
}

Conclusion

So with these couple Facebook SDK api calls, we have created a new Facebook Connect session and retrieve the current users information. Personally I think that this takes one more step than necessary, but the calls are pretty straightforward. The next post will go over getting the list of friends for our current user and then getting the Profile Picture photo album for each friend.

Code!

Here is a download of a small demo application that uses the steps that were just discussed. In order to run the application, you will need to set up your own Facebook application. Until next time, Sláinte!

FacebookMemoryPartI.zip (2.11 mb)



Comments

Deceptively Simple/Endlessly Complicated

How good is your Memory? Part II

How good is your Memory? Part II

02.09.2010 5:24 PM | Reply

Add comment




biuquote
Loading