Soak blog

Get the lowdown on what we love, what we hate and everything in-between.

adamcollison

08.12.2009

By: adamcollison

Under: Facebook Apps, Technical

Facebook Apps and Infinite Session Keys

When creating facebook applications there are a number of tasks involved that are not very well documented on the web, one of which is infinite session keys and how to use them.

Most of the literature available on-line is aimed at developers with a certain level of knowledge, as with most things its fairly straight forwards when you know how.

The Problem:
I need to update my FBML with a cron job but need to be logged into facebook to do so.

The Solution
Ok so this is where infinite session keys come into play, to run a cron job to update the facebook FBML cache (most probably in conjunction with fbml_setRefHandle) Here is what you need to do step by step.

Step 1
First login with your developer account that your apps being developed with.

Step 2
Visit this location below and replace the YOURKEY with your application apikey

http://www.facebook.com/code_gen.php?v=1.0&api_key=YOURKEY

Step 3
Once this screen loads up click on the “generate” button to generate a session key reference point, make a note of this somewhere safe.
(note this is NOT your infinite session key so keep reading)

Step 4
Create a PHP file on your server and include all of the standard facebook API classes (see example below)

<?php
include $pathTofiles . '\facebook.php';
$appapikey = 'APIKEY';
$appsecret = 'SECRET';
$facebook = new Facebook($appapikey, $appsecret);
$facebook->require_frame();
$user = $facebook->require_login();
?>

Step 5
Place in the following lines within PHP after you have all of the required Facebook calls working passing in the key given in stage 3.

$return = $facebook->api_client->auth_getSession('XXXXX');
echo $return['session_key'];

Step 6
Run this page in a web browser, it should generate on screen an encrypted key, this is your infinite session key so keep this safe and comment out the two lines mentioned in step 5.

Step 7 – Using the session key
Now you have your session key you can run something similar to following code on a cron job.

<?php
$pathTofiles = 'YOUR PATH';
include $pathTofiles . '\facebook.php';
$iSessionKey = 'XXXXX-XXXXX';
$appapikey = 'APIKEY';
$appsecret = 'SECRET';
$facebook = new Facebook($appapikey, $appsecret,$iSessionKey);

if(!$facebook->api_client->fbml_setRefHandle('myHandle','NEW DYNAMIC CODE HERE'))
{
// Perhaps mail site admin with an error?
}
?>

Finished
That’s about it, as mentioned earlier its a little confusing when you read the wiki and forums as its assumed you have a certain level of how this works.

Happy app developing!

Leave a Reply