Updating Facebook Profile FBML using setRefHandle, fb:ref and "infinate sessions".

Setting up an infinate session in CF is not difficult.

You need to echo the request.session_info.session_key to the screen and save it (somewhere). You also need a user id.

I have a scheduled task that triggers a file every hour, on the hour.

I like the setRefHandle because it allows me to update a cache on the facebook server instead of pulling or pushing content whenever there is an update. Even if the handle is the userID, the functionality is great!

In this examle, the handle name I have chosen is called "global_message" and the content is the date and time when the script was last accessed.

<cfscript>
facebook    = CreateObject("component","FacebookRestClient").init(argumentCollection=request.config);
request.key          = "[saved session ID]";
request.uid          = "[userID]";
</cfscript>
<cfoutput>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<InvalidTag http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<cfscript>
      res = facebook.fbml_setRefHandle("global_message", "#DateFormat(Now())# #TimeFormat(Now())#", request.key);
</cfscript>
</body>
</html>
</cfoutput>

The function to add to the facebook CFC is this:

<!---
Created by Cory Johnson on June 13, 2007

For help with this, contact cory@dragonflybytes.com
--->

<!--- cache controls --->
   <cffunction name="fbml_setRefHandle" access="public" output="false" returntype="any">
      <cfargument name="app_handle" type="string" required="yes" />
      <cfargument name="fbml" type="string" required="yes" />   
      <cfargument name="sess_key" type="string" required="yes" />      
      <cfset var result = "" />
      <cfset var params = StructNew() />
      <cfset params["handle"] = arguments.app_handle/>
      <cfset params["fbml"] = arguments.fbml/>
      <cfset params["session_key"] = arguments.sess_key/>
      
      <cfset result = callMethod("facebook.fbml.setRefHandle", params) />

      <cfreturn result />
   </cffunction>

In my facebook application, I have set the default FBML to this"

<fb:ref handle="global_message" />

Comments
quick question
I am trying to make my first app
I made the coldfusion output correctly
im just trying to figure how to push it to the profile.
what am i doing wrong? says No content to display.
# Posted By Drew | 7/19/07 11:59 AM
Did you setup the default FBML in your application settings?
# Posted By Cory | 7/25/07 6:09 PM
Hi Cory,

Thanks for all the info you've posted - it's a great help to guys like me that are trying to get their heads around how Facebook works.

I've created an application on Facebook, and want to automatically update the content of the applications profile box under the users profile.

In your post you mention: "You need to echo the request.session_info.session_key to the screen and save it (somewhere). You also need a user id."

Could you please explain this a little further? "What's the exact process to echo the request.session_info.session_key to the screen and save it (somewhere)"?

And what does the user id refer to? The Facebook users' userid? Or something to do with the coldfusion script running on my server?

Do I need to run this script (via a scheduled task) once per user that's added my application, or just once for the application (which will then update the apps' profile content for each user that has added the application)?

Sorry for all the questions - thanks again for your help!

Cheers
jb :)
# Posted By jb | 8/27/07 5:42 PM
Hey Jb

Really, all this means is that you need to save the session key variable somewhere, like a database or a file. (in the sample application this is request.session_info.session_key) So, you can try a <cfoutput>#request.session_info.session_key#</cfoutput> after logging in to see it output on the screen. In my own applications, I store this key in a database table. you only need to do this once, so it could be your own session key. No one else will ever see it, unless you put it on the screen for them (I can't imagine why you'd do that, but it is possible).

When running the updates to the facebook server-side data cache, you only need one user ID to execute the script (preferably your own) that user has to be logged in (using the session key you saved earlier).

To run this on a schedule, you need to setup a scheduled task on your CF server, that points at the CFM which has the code to log a user in (again, preferably your own ID, and Key) and it said functions will help you out updating the facebook server cache.

When *naming* the variable that is storing the content on the facebook cache, you can use a user's facebook ID as the variable name (if you only want to send custom content for that user). If you want to update a bunch of peoples profiles, you could use a more global variable name for the storage.

Drop me a line if you need more details :)
# Posted By Cory | 8/27/07 7:23 PM
Thanks heaps for the info Cory. Will try this out later on today when I have some more time!

Cheers
jb :)
# Posted By jb | 8/27/07 8:17 PM
related but kinda not related. Wondering if you know of a way to get the number of friends your frind have. Using the FQL to get it from the friends table doesn't work.

The only option i see is doing a CFHTTP request on the facebook.com/friends.php?id=xxxx b ut that seems like a bad way to go about it and an easy way to get on facebook's bad side
# Posted By Sean Scott | 8/31/07 4:03 PM