Integrating with Facebook on Windows Phone 7

I recently helped a collegue with Facebook integration on Windows Phone 7. The requirement was to log in and post a link to the users stream. I quickly came across the Facebook C# SDK found on codeplex at: http://facebooksdk.codeplex.com/. Unfortunately the otherwise pretty straight forward API is poorly documented, so it took me a bit of trial and error to get what I wanted. Here are the steps I took:

I grabbed the package containing both assemblies, source and last but not least samples. After unpacking I opened up the “CSWinPhone7SimpleApp.sln” solution from the Samples directory. At first the sample woulden’t build becaus of missing references. I fixed that by building the “Facebook-WP7.sln” solution found in the Source directory and referencing the resulting .dll’s. I then proceeded to launch the application in the emulator and got an error message in a small browser window.

It turns out the first thing you have to do is register you app with Facebook and get an app id. Go to http://www.facebook.com/developers/, log in and click “+ Set Up New App” in the upper right corner.

With the app id pasted into the sample, the application started and I was presented with a login screen. After filling in my credentials and hitting “Login” I was presented with my personal details as entered on Facebook.

Next up was the task of posting to the event stream. The documentation page found here http://facebooksdk.codeplex.com/wikipage?title=Code%20Examples&referringTitle=Documentation, shows basic usage, but it turns out the “Post Publish” example dosen’t compile on Windows Phone becaus it makes use of the dynamic keyword that isn’t available on Windows Phone. Going back to the example app it turns out that there’s a different syntax for Windows Phone:

Instead of using dynamics like so:

var client = new FacebookClient("my_access_token");
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.example.com/article.html";
parameters.picture = "http://www.example.com/article-thumbnail.jpg";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
parameters.description = "Longer description of the link";
parameters.privacy = new {
	value = "ALL_FRIENDS",
};
dynamic result = client.Post("me/feed", parameters);

Use dictionaries like so:

    private void Post(FacebookClient _fbClient)
    {
        var client = _fbClient;
        var parameters = new Dictionary<string, object>
           {
                { "message", "Olav is testing Facebook C# SDK" },
                { "link", "http://facebooksdk.codeplex.com/" },
                { "picture", "http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=facebooksdk&DownloadId=170794&Build=17672" },
                { "name", "Facebook C# SDK" },
                { "caption", "http://facebooksdk.codeplex.com/" },
                { "description", "The Facebook C# SDK helps .Net developers build web, desktop, Silverlight, and Windows Phone 7 applications that integrate with Facebook." },
                { "privacy", new Dictionary<string, object>
                    {
                        { "value",  "ALL_FRIENDS" }
                    }
                }
            };

        client.PostAsync("me/feed", parameters);
    }

Last but not least i had to add the “publish_stream” permission to the list of permissions stored in the _extendedPermissions field in the sample application (available permissions can be found here: http://developers.facebook.com/docs/authentication/permissions/).

And finally here’s what it looks like on Facebook:

And thats all there is to it. Basic Facebook usage on Windows Phone 7 – nice to know for future projects.

About these ads

Tags: , , , ,

6 Responses to “Integrating with Facebook on Windows Phone 7”

  1. RedDEvil Says:

    Hi.. Nice post.. very helpful,
    But I am experiencing dificulties in

    client.PostAsync(“me/feed”, parameters);
    Getting an exception here…

    Can You please provide the whole Publish stream part and are you navigating to another page for posting or ???..Can you please elaborate on Publishing..
    Thanks

    • Olav Rask Says:

      Since i just changed job i seem to have misplaced the sourcecode, but so far as i remember theres a Completed event on the client object. Hook up to that en have a look at what kind of error is in the argument maybe? Or if you’r not event getting that far maybe post the exception you are experiencing..

  2. Mayank Taneja Says:

    awesome!!
    can you please post the code for accessing the friend list and stuff like that using the new sdk.
    thnx a lot

  3. Moni Says:

    Hi.. this post very helpful,
    but i am facing some problem after calling
    the client.PostAsync(“me/feed”, parameters);
    not able to see any change on “FacebookLoginBrowser” control.
    just see the success message.
    please help to resolve this

  4. Pete Stockley Says:

    After you set up a new app, which of the following options did you select as I cannot get past the log in screen on my WP7 app.
    Current options (16/11/2011) include:
    -Website
    -App on FB
    -Native iOS
    -Native Android
    -Page Tab

    There is no native WP7 app option available.

    Thanks!

  5. momo Says:

    exactly what i needed, thanks for sharing !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: