OK - so for now I know nearly nothing about writing Facebook applications - but we need to start somewhere. I'll start with noting the problems I encounter while writing my first application (and I'll be adding solution as well I hope :). I guess this makes it a \WikiAsYouLearn page.

The application I write needs to be accessed both from Facebook and form outside.

Introduction

The way Facebook integrates with external applications can be compared to how a proxy gate can serve pages produced by external servers. It passes the web browser request to the application server and then the page produced back to the web browser. It works just like that but:

  • the external page is not served as it is to the client - but rather is a partially transformed and then inserted into the Facebook layout
  • all requests from the browser are passed ad POST requests to the app server - this is because Facebook always adds additional parameters (like the facebook id of the user) to the request

This means that the page produced by the application can contain additional Facebook XML tags that are interpreted by Facebook and transformed for bigger HTML chunks, and also cannot contain some HTML tags, but the parameters, relative links work just as if the user was accessing the application server directly.

When producing the page the application server can use it's own databases and additionally it can contact Facebook using the REST api to get additional info.

Prerequisites

Catalyst::Plugin::Facebook based on WWW::Facebook::API

Canvas sensitive uri_for

uri_for needs to return

http://apps.facebook.com/my_application/action

when called from inside Facebook canvas and

http://my.server.com/action

when called outside.

Solution

For now I try to just not use 'uri_for' - i.e. everything with relative paths. The only I encountered problem was with the create and update actions - the code for them is nearly identical, but the create action needs to redirect to 'id/the_new_pk/view' while update redirects only to 'view'.

canvas sensitive redirect

Similarly redirect needs to do:

$c->res->body( '<fb:redirect url="http://apps.facebook.com/my_application/new_place" />' );

when in canvas and standard:

$c->res->redirect("http://my.server.com/new_place");

when outside.

Solution

For now I just added the following sub to the main application class:

sub facebook_redir { 
    my ( $c, $uri ) = @_;
    if (! $c->facebook->canvas->in_fb_canvas()) {
        $c->res->redirect( $uri );
    }
    else {
        my $redir = qq{<fb:redirect url="$uri" />};
        $c->res->body( $redir );
    }
}

now I can use it like that: $c->facebook_redir( 'view' ).

I wander if there are any arguments for or against adding methods to the main app class like that.

mocking up a facebook request (for testing)

I have not yet found any good way to do that.

avoiding round trips to facebook

I've observed that Facebook sends the list of friends in the 'fb_sig_friends' POST variable. So instead of calling $c->facebook->get, which makes a request back to Facebook to get the list of friends I just do:

 my @friends = split /,/, $c->req->params->{fb_sig_friends};

Testing at development time

While developing your Catalyst/Facebook application, it's handy to be able to run it on your development box. There are a couple of methods:

  • If your development machine has a resolvable address, just set your canvas URL to your development box's address. You may want to use a non-standard port, e.g.:

    http://host.mydomain.org:8765

    My development box isn't directly accessible, but I use a dynamic DNS provider so my broadband router is accessible. I just port forward the non-standard port to my dev box.

  • If your development box isn't accessible, you may be able to use an SSH tunnel to your web server or other Internet accessible system. From your development box:

    ssh me@myserver.mydomain.org -N -R *:8765/localhost:3000

    Set the canvas URL to http://myserver.mydomain.org:8765.

    Run script/myapp_server.pl as usual.

-- Zbigniew Lukasiak, 2008-01-24 17:18:13

My tags:
 
Popular tags:
 
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker