Quantcast
Channel: SmartyPantsCoding.com - Facebook
Viewing all articles
Browse latest Browse all 2

Using the Facebook Developer Toolkit With Windows Phone 7

$
0
0
AttachmentSize
FacebookConnectWindowsPhone.zip453.06 KB

A couple of people have noticed that the Facebook Developer Toolkit doesn't work on the current release of Windows Phone 7. The primary reason is that it has a dependency on System.Windows.Browser - a library which isn't in the WP7 runtime (Silverlight isn't hosted in a browser on the phone).

I took the source from the Facebook Developer Toolkit and hacked at it until I got it to work on the phone. Here is a quick demo showing it working in the WP7 emulator:

(click 'read more' for more on how this was done..)

Modifying the Silverlight Version of the Facebook Developer Toolkit
First, you'll need to download the latest release of the Facebook Developer Toolkit, extract it, and open the Facebook.Silverlight solution. Next, remove the reference to System.Windows.Browser. Then, make the following changes:

BrowserSession.cs

  • Comment out the using line for System.Windows.Browser
  • Find the 'Script Helpers' and 'Helpers' regions and comment both regions out
  • Comment out the contents of the Login() and Logout() methods
  • In the BrowserSession constructor comment out the following three lines:
    HtmlPage.RegisterScriptableObject("FacebookLoginControl", this);
    InvokeScriptCall("facebook_init", ApplicationKey);
    InvokeScriptCall("isUserConnected");

RestBase.cs
Replace two lines of:
queryBuilder.Append(System.Windows.Browser.HttpUtility.UrlEncode(kvp.Value));
with:
queryBuilder.Append(Uri.EscapeDataString(kvp.Value));

Once you have made these changes you should be able to build the project successfully. The Facebook.Silverlight.dll now being built no longer carries a dependency on System.Windows.Browser. Note that the changes above in RestBase.cs may cause some side effects depending on what is being encoded, but I believe it is the best comparable function in WP7 currently.

Hooking up a WP7 application to the modified Facebook Developer Toolkit

By removing the logic in the BrowserSession.cs we broke how the Facebook Developer Toolkit normally gets a session setup with Facebook. To fix this we are going to setup the BrowserSession ourselves - once the BrowserSession is setup then using the rest of the Facebook API should be exactly the same as a normal Silverlight application.

The screencast in this blog entry has a general overview of the code, so please refer to that and the attached project for details, but this is the basic way this will work:

  • Host a WebBrowser control in your app and navigate it to your application specific Facebook Connect login URL
  • Listen to all WebBrowser.Navigate() events - these will occur when the user is redirected after trying to log in
  • Look for "auth_token=" in the WebBrowser's URL when the Navigate() event fires
  • Call Facebook.Rest.Auth.GetSessionAsync() and pass in the auth_token
  • Create a BrowserSession object and store the 'session_key' and 'secret' values turned from GetSessionAsync() into it
  • Use the BrowserSession object like normal for the Facebook Developer Toolkit

If you are going to try and do Facebook integration on the device I highly suggest you do your work in a normal Silverlight application before moving on to try on a modified version of the Facebook Developer Toolkit. Keep in mind that to do anything with Facebook Connect or their APIs you'll have to register an application and get an application key and a secret key. You can find the modified library named 'Facebook.SilverlightWP7.dll' in the attached sample project built off of the 3.01 source drop.


Viewing all articles
Browse latest Browse all 2

Trending Articles