Automatically Update all your User’s Facebook Profile
Wednesday, 27 of June , 2007 at 8:31 pm
While developing my first Facebook application, I spent a lot of time trying to get my application to auto-update everybody’s Facebook Profile. Majority of this time was wasted searching though discussion boards. Hopefully this will save you some grief.
You probably know by now that Facebook caches anything you post to a user’s profile. This means that you have to write a script which manually updates everyone’s profile. You might think that the reference tag would be the exception, but you would be mistaken.
Facebook will cache the following line:
<fb:ref url=”http://www.mysite.com/someurl.php” />
In order to clear the cache, you would have to execute this line:
facebook.fbml.refreshRefUrl(”http://www.mysite.com/someurl.php”)
Although using handles and references is a valid approach, I found it simpler to use profile_setFBML instead.
This led me to the next problem I ran into. When I started writing my auto-update script, I ran into the infamous “Invalid session key” error. In order to execute profile_setFBML, you need have a valid session key (you will need a valid session key for refreshRefUrl as well). This can be ANY session key! It doesn’t have to be the session key of the user’s profile you are updating.
Unfortunately, this isn’t as easy as using your own session key. Session Keys have lifespans, so we will have to create a special infinite session key. The first step is to create a new user for Facebook. Once you successful do that, install your application for your new user.
Next go to the following address and generate yourself an auth token:
http://www.facebook.com/code_gen.php?v=1.0&api_key=YOUR_API_KEY
Then pass your auth. token to your canvas:
http://apps.facebook.com/applicationname/?auth_token=YOUR_AUTH_TOKEN
When you are on your canvas, you will need to print your session key. You can do this by printing this:
<?php echo htmlentities($facebook->api_client->session_key); ?>
We can then use this key to update everyone’s user profile. Here is some example code (it should work).
Now that we have the script written, you will have to create a cronjob which will execute it on a given interval. I am not going to go into any details on how to create a cronjob. However, most web hosts allow you to do this easily.
As an important side note, you can’t use any infinite session key to publish a story or action (feed_publishStoryToUser or feed_publishActionOfUser). You will need a valid session key for the user you are performing the action on. This will force you to keep a record of valid session keys for all your users.
While developing my first Facebook application, I spent a lot of time trying to get my application to auto-update everybody’s Facebook Profile. Majority of this time was wasted searching though discussion boards. Hopefully this will save you some grief.
You probably know by now that Facebook caches anything you post to a user’s profile. This means that you have to write a script which manually updates everyone’s profile. You might think that the reference tag would be the exception, but you would be mistaken.
Facebook will cache the following line:
<fb:ref url=”http://www.mysite.com/someurl.php” />
In order to clear the cache, you would have to execute this line:
facebook.fbml.refreshRefUrl(”http://www.mysite.com/someurl.php”)
Although using handles and references is a valid approach, I found it simpler to use profile_setFBML instead.
This led me to the next problem I ran into. When I started writing my auto-update script, I ran into the infamous “Invalid session key” error. In order to execute profile_setFBML, you need have a valid session key (you will need a valid session key for refreshRefUrl as well). This can be ANY session key! It doesn’t have to be the session key of the user’s profile you are updating.
Unfortunately, this isn’t as easy as using your own session key. Session Keys have lifespans, so we will have to create a special infinite session key. The first step is to create a new user for Facebook. Once you successful do that, install your application for your new user.
Next go to the following address and generate yourself an auth token:
http://www.facebook.com/code_gen.php?v=1.0&api_key=YOUR_API_KEY
Then pass your auth. token to your canvas:
http://apps.facebook.com/applicationname/?auth_token=YOUR_AUTH_TOKEN
When you are on your canvas, you will need to print your session key. You can do this by printing this:
<?php echo htmlentities($facebook->api_client->session_key); ?>
We can then use this key to update everyone’s user profile. Here is some example code (it should work).
Now that we have the script written, you will have to create a cronjob which will execute it on a given interval. I am not going to go into any details on how to create a cronjob. However, most web hosts allow you to do this easily.
As an important side note, you can’t use any infinite session key to publish a story or action (feed_publishStoryToUser or feed_publishActionOfUser). You will need a valid session key for the user you are performing the action on. This will force you to keep a record of valid session keys for all your users.
Category: Programming, Tutorial
Trackback by myspacetotal
Made Sunday, 19 of August , 2007 at 3:12 pm
myspacetotal…
Interesting Article….
Trackback by golfmonster
Made Monday, 20 of August , 2007 at 2:05 pm
golfmonster…
Interesting article, thank you….
Comment by sliver
Made Friday, 31 of August , 2007 at 7:03 pm
Great article…one thing i don’t understand…How do you keep a record of valid session keys for users? To clarify…I know I can store them in a db, but at which point should i store the session key and how do I know it is good? Don’t they expire? Thank you much
Comment by admin
Made Sunday, 2 of September , 2007 at 4:35 pm
What I did was updated the user’s session key every time they go to the canvas. You can get the session key by using the following command:
$facebook->api_client->session_key
You then just update the user’s settings in your DB to the new session key.
When you want to use feed_publishStoryToUser or feed_publishActionOfUser, you will need to grap the user’s session key and try updating it. You will be thrown an error if it isn’t a valid key.
You would think that the session time-out would be a bigger issue, but it seems to rarely happen.
Comment by iDescribe
Made Thursday, 6 of September , 2007 at 4:08 pm
Thanks this article and specially the last comment by admin which cleared things up, was alot of help to iDescribe.
Comment by Oliver Nassar
Made Saturday, 3 of November , 2007 at 12:15 pm
Worked like a charm; thanks.
Pingback by Lista de recursos para desarrollar aplicaciones para Facebook « Think Free - Linux.Php.Java.ME.Movies
Made Friday, 4 of April , 2008 at 2:59 am
[...] Automatically Update all your User’s Facebook Profile [...]
Pingback by Imaginary Roots
Made Tuesday, 8 of July , 2008 at 5:30 am
[...] my previous post regarding updating the user’s profile box on a Facebook application, I found this site which shows you how to update everyone’s profile content in one go. Of course, this means its [...]
Comment by Agustin
Made Tuesday, 7 of October , 2008 at 6:53 am
Hello, im not quite sure if I’m understand.
In yor example code you grab de userID form a table… that table is in your host? or its a table in facebook?
if its a table un your host, I suppouse you first save the users in your Db… is this ok?
can you write the steps from the beginning?
thanks a lot man!
Comment by eldila
Made Thursday, 16 of October , 2008 at 11:05 pm
Well, the code snippet wasn’t meant to show you everything. The userId is in your own database. You will insert these values into your database when a user installs your app.





