header bg
 

Sl.ayer's layer

It looks like you're writing a blog...

Hosting Silverlight application in Facebook iframe

by Sl.ayer 17. December 2009 04:24

If you’re building a Silverlight application for Facebook, you have two choices: (1) build a stand alone application and use Facebook Connect for authentication, or (2) create a canvas application that will appear on a Facebook site and use the parameters passed to iframe to establish a session. The former is well covered in the Facebook Toolkit documentation, but I couldn’t find any examples of how to embed a Silverlight application within a Facebook iframe. It took a few days worth of research and experiments to get it right and I hope this post will be helpful to anyone who is working on Silverlight canvas application for Facebook.

Embedding Silverlight into Facebook canvas

I know three ways of embedding Silverlight application into a Facebook canvas.Fbss

(1) Select the IFrame option for Render Method in Canvas section of the application settings and embed the Silverlight application inside the iframed page using standard methods (object tag, silverlight.js, etc.).  (2) Use <fb:iframe> tag in fbml based canvas. (3) Use fbml tag <fb:silverlight>. The <fb:silverlight> tag would be the preferred method, but according to the Facebook wiki, the tag is not implemented at this time. That leaves only options 1 and 2 available – embed Silverlight in iframe.  Whether you go with IFrame canvas or use <fb:iframe> tag in fbml, the following steps are the same:

Extracting parameters passed by Facebook to iframe page

First lets take a look at iframe created by Facebook

<iframe frameborder="0" scrolling="no" name="fb_iframe_4b2894**"
  class="smart_sizing_iframe" smartsize="true"
  src="http://mysite.com/facebookapp/?fb_sig_in_iframe=1&
      fb_sig_iframe_key=c***f&
      fb_sig_locale=en_US&
      fb_sig_in_new_facebook=1&
      fb_sig_time=1***7&
      fb_sig_added=1&
      fb_sig_profile_update_time=1256693703&
      fb_sig_expires=1261108800&
      fb_sig_user=1***1&
      fb_sig_session_key=2***1&
      fb_sig_ss=b***&
      fb_sig_cookie_sig=f***b&
      fb_sig_ext_perms=status_update,photo_upload,publish_stream&
      fb_sig_api_key=e***6&
      fb_sig_app_id=2***0&
      fb_sig=0***2"
  style="width: 758px; height: 679px;"/>

As you can see, Facebook adds a bunch of parameters prefixed with fb_sig to the url. We don’t need to worry about all of them. The most important parameters needed to establish a connection to Facebook  are fb_sig_ss, fb_sig_session_key and fb_sig_api_key.

The way you extract url parameters depends on which technology you use.  I am using asp.net mvc for my development, but it should be trivial to change the code to work with plain asp.net or php.

public class FacebookController : Controller
{
    public ActionResult Index(
            string fb_sig_added, string fb_sig_user, string fb_sig_ss,
            string fb_sig_session_key, string fb_sig_api_key)
    {
        ViewData["ss"] = fb_sig_ss;
        ViewData["key"] = fb_sig_session_key;
        ViewData["apikey"] = fb_sig_api_key;
        ViewData["user"] = fb_sig_user;
        return View();
    }
}

Values passed by Facebook to our iframe was saved in ViewData, so we can pass them to the Silverlight application later. One value we do not pass to the application is fb_sig_added. The fb_sig_added parameter is important since it indicates if a user has authorized the application or not. If fb_sig_added is “0”, that means a user has not authorized the application and the rest of the fb_sig_ parameters will be empty. Only a limited set of Facebook APIs can be accessed by an unauthorized application, and I am not going to focus on that scenario.

Passing parameters to Silverlight application

Below is a sample of a striped down object tag with InitParams used to pass Facebook parameters to the Silverlight application.

<object data="data:application/x-silverlight-2,"
   type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="/MyApp.xap"/>
    <param name="InitParams" value="key=<%= ViewData["key"] %>,
      apikey=<%= ViewData["apikey"] %>,ss=<%= ViewData["ss"] %>,
      user=<%= ViewData["user"] %>" />
</object>

Retrieving values from InitParams

To retrieve values passed in InitParams, we need to add a bit of code to the Application_Startup handler in the App class.

private void Application_Startup(object sender, StartupEventArgs e)
{
    if (e.InitParams.ContainsKey("ss"))
    {
        FacebookFx.SessionSecret = e.InitParams["ss"];
    }
    if (e.InitParams.ContainsKey("key"))
    {
        FacebookFx.SessionKey = e.InitParams["key"];
    }
    if (e.InitParams.ContainsKey("user"))
    {
        FacebookFx.UserId = Convert.ToInt64(e.InitParams["user"]);
    }
    if (e.InitParams.ContainsKey("apikey"))
    {
        FacebookFx.ApiKey = e.InitParams["apikey"];
    }
    this.RootVisual = new Page();
}

Creating Facebook session and retrieving list of friends

The last step is to use parameters we received from Facebook to establish a session and make a call to retrieve the list of friends of our current user.

static public void Init()
{
    Facebook.Session.BrowserSession _session = 
        new Facebook.Session.CachedSession(ApiKey, SessionKey, SessionSecret); 
    _session.UserId = UserId; 
    Facebook.Rest.Api _api = new Facebook.Rest.Api(_session); 
    _api.Friends.GetAsync(GetFriends, this);
}

static public void GetFriends(IList friends, object state, FacebookException e)
{
    if (e == null)
    { 
        List fs = new List(friends);
        …
    }
} 

Getting more information

Silverlight is the new kid on the block as far as Facebook is concerned. Fortunately, many of the same issues concerning Silverlight apply to Adobe Flash as well. For example, I was able to find several articles describing process of embedding Flash application into a Facebook iframe, and other than minor differences in how parameters are passed from web page to the application, the information was still relevant to Silverlight.

Comments (12) -

Erc
Erc
12/19/2009 1:26:36 AM #

Thanks much for writing this up. I am trying to do exactly what you describe as objective, write a Silverlight app in a Facebook iFrame.

Would you be open to post your complete solution as an example to use as starting point?

If that is not possible, did you star from a specific solution in the Facebook Developer Toolkit sample?

Admin
Admin
12/19/2009 11:41:55 PM #

The project I am working on would not make good starting point since it is quite big.
As far as I know FDT doesn't have any starter Silverlight projects for applications inside the Facebook iframe. I might create one at some point if I have free time and if I see enough interest.
The goal of this post was to provide all of the information necessary to implement Silverlight app in Facebook iframe. Is something missing or not clear?

Erc
Erc
12/25/2009 4:20:22 PM #

Thanks for the reply.

I assume the post is complete but I am just starting to learn about Facebook apps so it is not obvious to me how to get started.

I program only as a hobby. I am proficient enough in Silverlight but all the Facebook stuff is new to me. Your post is very helpful connecting the two pieces.

Tyler
Tyler
12/29/2009 7:47:32 PM #

I found that the Async callback has to dispatch to the UI thread like so:

public void GetFriends(IList<long> friends, object state, FacebookException e)
{
  if (null == e)
  {
    List<long> fs = new List<long>(friends);
    StringBuilder sb = new StringBuilder();
    foreach (long f in fs) sb.AppendLine(f.ToString());
    Dispatcher.BeginInvoke(() => FriendText.Text = sb.ToString());
  }
  else
    Dispatcher.BeginInvoke(() => FriendText.Text = e.Message);
}

matt
matt
1/16/2010 4:35:22 AM #

I'm trying to understand how you accomplish this here. Are you managing the session in the browser by inheriting from the appropriate FDT Page object?  You are creating a cached session object which I assume means the session had to have already been established?

Admin
Admin
1/16/2010 8:32:56 PM #

@Matt
I don't use FDT for my site, I only use it in my Silverlight appliacation. It should be possible to use FDT IFrame based page to get session inforamation, but I didn't do any research in that direction.
Using cached session works for 2 reasons: (1) because user is logged in to Facebook (session exists) and (2) Facebook passes session information to iframes located inside canvas (i.e. Facebook shares its existing session with our application, so we don't need to create a new one)

bambang tri
bambang tri
2/7/2010 11:57:37 AM #

Can you mention an example of facebook application that use silverlight ? I'm curious how it'll look like.

Admin
Admin
2/7/2010 3:11:52 PM #

This post is the result of my work on http://apps.facebook.com/comiccomposer/.

ruell
ruell
3/31/2010 6:23:53 AM #

Hi,

Very useful article indeed but i have a question.
are there any other files needed to make this work in silverlight?
most of the examples i found on the internet uses fblogin.js and
xd_receiver.htm, are these files needed in your sample or facebook SDK will just work without these files?

Thanks

Admin
Admin
4/1/2010 7:08:47 PM #

@Ruell, Canvas application don't need to use facebook connect (fblogin.js, xd_receiver). Only standalone applications need that and most samples of silerlight applications are standalone apps.
fb_sig_ss is not deprecated and is safe to use.

Guru
Guru
4/20/2010 5:10:08 PM #

I have developed an application using this approach. But now i am stuck as i want to use FBML publish stream function. which i can call only from my parent frame. I wanted to write a javascript method in parent frame and call from my iframe. But FB is not allowing me to do so.

Do you know of any workaround for this?

Admin
Admin
4/20/2010 7:42:56 PM #

@Guru
You might consider asking user for extended permissions to publish stories without individual approval.

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading






header bg