Wednesday, August 10, 2011

Facebook Page Tab Canvas IFrame Application with CakePHP

Ahhh, the joys of Facebook development!

I remember back in 2007 when they launched _THE_ Platform. Man was it a weird thing. At the time I worked on a Flash prototype but quickly gave up. If only I would've persevered maybe Farmville would have a different owner :D
Last year I tried again. FBML, page scraping, localhost dev with port forwarding, lakes of tears...

Now it's take 3.
It definitely looks easier.
Especially after this year's shift to IFrame apps. If you dont want to look under the hood you don't. If you want to or have to, the only two not-so-easy parts are dealing with callbacks and OAuth2.0.
And as always when it comes to PHP it's time to put CakePHP to work.

But I have to admit that the Cake did it to me again...
The Session component clears the existing session when the Session.start is true in config. And it was.
So the Facebook user id was there only on IFrame load and it was deleted immediately after by the SessionComponent. Setting Session.start to false fixed it but I don't want to do that.
The solution that works is ditching the facebook.php and extending FacebookBase into a CakePHP Facebook Component. Biggest downside is the Security.level set to low so that CakeSession doesn't insist on a session.referer_check which breaks the FB flow.
The whole thing is server side only. This is mainly because at the time of the coding the PHP SDK docus clearly yelled that it is not compatible with the the to-be-updated client side JS SDK.
But it works great. Unltil they change something on FB. Btw, did anyone see the Test users tab in the Roles section that I used a while ago? Ah, there it is! Back again after a week in the countryside :-D

In the code you can also find some hints at the "Publishing to Page feed from Facebook Application" problem.
The issue was grabbing an offline auth_token for the Facebook page so that my application can make posts to the Page wall at any time. This is achieved by having a FB permission section in the admin where the admin who also is an admin of the FB page can initiate the access_token request. The component's updatePageAccessToken method is called and that redirects the user to the FB login and then App permissions pages so that after a successful flow Facebook calls back the UsersController admin_facebookPermissions method that checks the component getPageAccessToken() and stores it if it's there.

Although the component is not a Swiss Army knife for Cake Facebook development I think it's worth sharing. It's not a plugin so not as plug and play as some may desire but it can be used without major hassles.

Questions are as always welcomed.


Post a Comment

<< Home