Over a million developers have joined DZone.

Facebook Integration In A Windows Phone 7 Application

DZone 's Guide to

Facebook Integration In A Windows Phone 7 Application

· Mobile Zone ·
Free Resource
Facebook is unavoidable social engagement tool. If you are building a Windows Phone 7 application for a certain event, celebrity or anything that has a associated Facebook page, you should consider adding some sort of engagement with it in the app. One example is adding the most recent posts in some format to your client application. However, you cannot simply retrieve data from Facebook publicly, calls need to be authenticated first and for that we need to create a Facebook application first.

This post will show how to fetch the most recent posts from the page using only RestSharp. If you are building something more complicated with more integration, consider using this awesome library – Facebook C# SDK.

You can do that on the Facebook developers/Apps page and click Create New App and give your application a name. For the testing purposes, use some random app name.

 Creating new Facebook application

Once your application is created, you will get your App ID and App Secret required for making authenticated calls. Base URI for all requests is https://graph.facebook.com Note that I will use Async CTP 3 in this application and RestSharp async extensions mentioned in the previous post.

First we need an access token. You can retrieve it via GET request on the following URI: /oauth/access_token?type=client_cred&client_id=APPID&client_secret=APPSECRET (append to the above mentioned URI). You should get a response and its content should look like:


You will authenticate all calls from now on with the given access token. Let’s retrieve news freed for Windows Phone Facebook page by fetching data from the following URI: /windowsphone/feed?access_token=YOUR_ACCESS_TOKEN.

Simply append the entire content from the previous request to the end of the URI. The full code looks like this:

private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    // define base URL
    _restClient = new RestClient("https://graph.facebook.com");

    // cache token between calls (if you consider adding refresh button or auto refresh)
    if (String.IsNullOrEmpty(_token))
        var tokenRequest = new RestRequest(string.Format("/oauth/access_token?type=client_cred&client_id={0}&client_secret={1}", AppId, AppSecret));

        // token will be right inside
        _token = (await _restClient.ExecuteTaskAsync(tokenRequest)).Content;

    // now fetch the list of posts
    var request = new RestRequest(string.Format("/{0}/feed?{1}", "windowsphone", _token), Method.GET);
    DataContext = await _restClient.ExecuteTaskAsync<Data.FacebookData>(request);

If we run the code now, we will get the current news feed as seen on the following image (please note that this is rather old screenshot):
Fetched data from the official Facebook page

Classes used for deserializing the response are:

public class FacebookData
    public List<FbMessage> Data { get; set; }

public class FbMessage
    public string Id { get; set; }
    public FbFrom From { get; set; }
    public string Message { get; set; }
    public DateTime CreatedTime { get; set; }

public class FbFrom
    public string Name { get; set; }

Since I wanted to show this as a concept, there aren’t a lot of fields added. Feel free to add more fields if you need them. You can play with the Graph API Explorer and examine which endpoints you have and what kind of data do they return.

It is quite obvious from the image above that the basic text output looks bad and you should spend some time converting to either RichTextBox or WebBrowser control for data representation. I will leave it as an exercise (poor excuse for laziness) for the reader. With this code you can easily embed Facebook feed in your Windows Phone 7 application.

You can download the source code from the following location: link [SkyDrive 19.5 KB].


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}