Easily integrating your Mojolicious app with Facebook.

Facebook has recently released a new Graph API. It’s a simple RESTish API, and uses the newly released OAuth2 spec for authorization. This makes it a great match for Mojolicious, and using my new OAuth2 plugin (Also coming to a CPAN mirror near you), it’s absolutely trivial.

Here is a simple Lite example. Just register an app with facebook to get the key and secret.

   use Mojolicious::Lite;
   # configure the plugin
   plugin 'oauth2', facebook => {
       key => 'my-key',
       secret => 'my-secret' };

   get 'hello' => sub { 
      My $self=shift;
      #redirects the gets the token asynchronous   
   $self->get_token('facebook', callback => sub {
      my $token=shift;
      my $me=$self->client->get(
        'https://graph.facebook.com/me?access_token='.$token)->res->json;
          $self->render( text =>
            "Hello ".$me->{name} );
    });

As you can see, the plugin handles the request flow automatically. We then use the built in JSON parsing in the Mojolicious Client to turn the API data into structures we can work with in our application.

Updating facebook data is just as trivial, you just use post requests with the data you need to update. See http://graph.facebook.com/ for more info on that.

If you plan to run your app through a standalone daemon such as Hypnotoad (I really think you should :) It could be worth it to rewrite the last section of your app to take advantage of async operations like this:

  $self->client->async->get(        'https://graph.facebook.com/me?access_token='.$token, sub {
  my $me=shift->res->json;
  $self->render( text=>"Hello ".$me->{name} );
})->start;

The async plugin itself also supports an optional async=>1 setting to enable async fetching of the token.

iPad as a writer’ s tool

I believe I have found my ideal writing tool. For the last couple of months, I have been drafting text using iA Writer on the iPad. While the softward keyboard is cumbersome to use, I find the focus the combination gives very inspiring. Nothing but me and the text, no IMs or emails to disturb me. I have turned off notifications on the iPad, so not even a ongoing game of Words With Friends can spoil my productivity.

Today I was out shopping on Oxford street, and on a whim I picked up the iPad keyboard dock. This thing is awesome! It has the required special keys to interact with the iPad’s functionality, but otherwise it feels just like Apple’s other excellent keyboards. It holds the iPad vertically in just the right angle, and plugs into a power source, as well as external speakers if I need music while I work. In short, I am in love.

I expect this to be my primary writing tool for the near future. If I’m going to nitpick, the only thing I am hoping for is higher DPI on the next generation of iPads so that iA Writer can fit a bit more text per line. One final trick; The keyboard lets you switch between languages with cmd-space. Exactly the same combination I use to switch between norwegian and english keyboard layouts on the mac. Pretty awesome.

Copyright © marcus ramberg
nordaaker

Built on Notes Blog Core
Powered by WordPress