5 Development Tips for the Facebook API

Below are five tips on using Facebook's API. These techniques were all used in TripConnect's Travel Profile Application that allows Facebook users to add a travel interests to their Facebook profile. I know they work in this app, but they may not be the perfect solutions.

The first two tips are specific to the JAVA kit. The others are more general.

1) Installation - I saw several posts in the Developer Forum on users having trouble installing the Java kit in Eclipse. I didn't have much of an issue . All that was needed was to create the com.facebook.api package in the src folder, move all the java files into this folder, drop the two jar files (browselauncher and json) into my libraries folder, add these libraries to my ANT User libraries and build... No issues.

2) FacebookRestClient.java -does not have a method to pull the userid. So after calling facebookRestClient.auth_getSession(authToken) to establish the session, I found no way to get the userid. Odd... All I did was add one method:

public int getUserId() { return _userId; }

to FacebookRestClient.java

3) What to do with the Session id - Store this in your application's session manager, but don't store it forever (e.g., don't store it in the database). You can try to ask the user to 'remember me', but there is no guarantee that the user will do this for you. The JAVA api doesn't gather or store the expiration value for the session so if you want to be slick and test for expiration, you'll need to modify the API to collect it for you. Regardless, if the session expires or you just don't have one for this user, then you need to redirect to

http://www.facebook.com/login.php?api_key=YOUR_API_KEY&v=1.0

as stated in the authentication guide.

4) setFbml - is the call you have to make to set content on a User's Profile Page. We found the best way to set content is to use fb:ref and pass in a URL that you host for the content that you want to display. fb:iframe will not work for the profile object. Note that the contents of the URL that you pass are stored in a Facebook cache.

In the developer's forum, many users have asked about how to update/reset the cache. Some have tried doing this w/ a script on cron and storing session id's. Even if you get this to work (I haven't seen a fool proof approach), you are creating a potential scalability issue if your lucky enough to have lots of users sign up for your app.

The best method is to have the app update the obect if/when users access your app. Facebook provides an Edit Url which you can use to point to a canvas page with links to update the application data. Once the user does an update, your application can reset the cache. Here's how we do it:
  • Edit URL points to a canvas page with a bunch of links on tools users can enhance their travel profile.
  • Each link points to http://www.facebook.com/login.php ... . The next attribute in the query string is used to point to the tool in the TripConnect application.
  • Since your inbound links are bouncing through http://www.facebook.com/login.php , you'll get an auth_token parameter which can be converted to a session id.
  • Once you have a new/valid session id, you can call facebookRestClient.fbml_refreshRefUrl(URL) (which will update the cache) followed by a facebookRestClient.profile_setFBML("<fb:ref url=\"" + URL + "\"/>") (which insures that fbml for the user's profile is set correctly.
It's not perfect but should work in many situations.

5) Your callback URL - The easiest thing to do with the Callback URL is to redirect it back to
http://www.facebook.com/login.php which will give the user the opportunity to add the application. But doing this blindly and without checking the current state can lead to problems. For example, if the user already added the app and an auth_code was passed in, then redirecting back will cause a message to appear to the user - "You already added this app". Our logic on this callback URL does the following:

If an auth_code was not passed in then redircect back to http://www.facebook.com/login.php
else if the user is already a member, then render an appropriate page
else (user is not a member and auth_code was passed in) then send the user to the Add App page. (http://www.facebook.com/add.php?api_key=YOUR_API_KEY

Regarding the last use case - this seems to be either a defect or oddity in Facebook's authentication which can happen if the user adds the app, then removes it, then tries to add it again. Since the user added it once before, Facebook doesn't present the page to add it again.

One issue with the last use case is the add.php is a full page (w/ nav...), but the call to your Callback URL is supposed to return only the content frame. You'll need to frame bust your response using something like the following:

<meta http-equiv="refresh" content="0;url=http://www.facebook.com/add.php?api_key=YOUR_API_KEY"/>
<script type="text/javascript">
if (top.location != location) top.location.href = location.href;
</script>

Updated June 14, 2007: The facebook wiki has updated documentation on the callback URL.

The callback page now does, theoretically, double-duty. In the previous world of the Facebook API, your callback page would get called back when the user visited login.php, and it would be a GET with a &auth_token= (and whatever else you wanted to pass around). If, however, the user visits apps.facebook.com/yourapphere/, then the callback is called to serve the "canvas". In that case, it gets a POST.
See Your Callback Page for all the details.

28 comments:

  1. hi dear,
    your above info. is much more helpful to me but one more thing i had tried facebook_java_client library to configure in my eclipse ide but i am getting 100 + errors, i have followed steps from videos i found on this url. so please let me know where am i going wrong ?

    http://www.thescreencast.com/2007/06/getting-started-with-facebook-platform.html

    Thanks,
    nekin

    ReplyDelete
  2. Hi,
    What you write in this blog is very interesting. I'm Happy to find your blog.
    I really need this information.
    Thanks.

    Exchange Links Please!

    Wisata SEO Sadau

    ReplyDelete
  3. Anonymous1:14 AM

    Your Best Choice! wow power leveling and wow gold wow gold|*|wow power leveling|*|http://www.saleveling.com|*|fgf554

    ReplyDelete
  4. Anonymous3:53 PM

    1。那混合物是更缓慢的 ... 但是 Lexus 的即将到来混合版本 ' 将是比气体气体更快的唯一的版本如好地有多马力。不要自夸速度,但是我被吸引轮流开送行为 90,是警察给我一次休息。
    2。那不是很
    ... 只是通过在城市乘公交车往返我储蓄过来 $ 5000/yr 与我的以前的汽车,吉普车切诺基相比。超过 5 年,会是 $ 更不用说会进一步增强我的储蓄的最近的比率远足的 20K。这样除非你是在你的父母的地产上吸的一个浪费的儿子,你的声明是一束公牛。

    ReplyDelete
  5. Anonymous3:53 PM

    3. 45 (90 r/t)
    45mpg 天是 2 我的车上> 8 加>>比。那每天是 6 >仑的一笔>蓄, 120 月, 1440 每年者 5040 (根 3.5 元/) ... 加上它发表 1/10th CO2。多愚蠢是它不要骑一个,去算进今天和年龄。
    4.缺少了解 ... 是真的,实际上我个人这样那样喜欢它我可能享受所有鼓励;税,合伙用车,免费停车米, prius 业主之间的秘密的信号,等等;这样自私地说那我真地在那里在享受在所有气体汽车业主上的所有权那没有一个想法多少我这辆汽车有的嬉戏。我 junked 我的 SL,郊区对我的 Prius ... 你应该也。

    ReplyDelete
  6. Check out our blog for all types of shopping cart and ecommerce solutions at extremely affordable rates. Contact to get disocunts and your online shop Now!

    ReplyDelete
  7. Thanks for the information, we will add this story to our blog, as we have a audience in this sector that loves reading like this” web development

    ReplyDelete
  8. Thanks for the information, we will add this story to our blog, as we have a audience in this sector that loves reading like this” ecommerce solutions

    ReplyDelete
  9. nice post abou this facebook to midify. And the basic thing is using PHP its important also.

    ReplyDelete
  10. Can you please explain a point after I redirect a user to add.php?api_key=... How would I know which page has added the application? May I know it? I mean is there any way to get callback from there and if yes how can I get the page id in which the application is added?

    ReplyDelete
  11. Thanks for the interesting development tips!!!

    ReplyDelete
  12. Hi, Some one from facebook refereed your link i have book marked it nice blogs you write see Free  how to grow twitter followers here

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Hi Isaac, You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality tips regarding
    facebook development
    and really i mean that.

    ReplyDelete
  15. An inspiring and wonderful face book application developement with new ideas.
    thanks,
    Facebook for business
    Facebook iframe
    Facebook app development

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Hello Isaac Sacolick i was following you for long a time ago and i found your each blog post is very informative this time you discussed highly trending topic that is Facebook thanks man for discussing Facebook Application Development tips.

    ReplyDelete
  18. Hey Isaac i am really impressed by you and the post is brilliant.

    Facebook App Development

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Hi,
    What you write in this blog is very interesting. I'm Happy to find your blog.
    I really need this information.
    Thanks.

    Facebook Application Development

    ReplyDelete
  21. Hello, your development tips for the Facebook API sounds great. I had a great time reading your post. I will recommend this also to my friend. Big thanks and keep posting.

    ReplyDelete
  22. The development tolls for Facebook application is given in the post here. Know all about it

    Website designers

    ReplyDelete
  23. The visitors will surely considered this post as best. I also get lot of information from the web development tips article.

    Web Designing India

    ReplyDelete
  24. Anonymous5:17 AM

    This was obviously so funny. How can he say that online. Were the statements really true? If it was true or even almost true then they should have checked their systems to see if someone had hacked in.

    galaxy note 2 screen protector

    ReplyDelete
  25. Nice development tips for facebook.

    PHP Development Company

    ReplyDelete
  26. Balliro Commerce Group offers premier design and development services, best website developer

    ReplyDelete
  27. hi pls can you explain in this functions ( http://www.facebook.com/login.php which will give the user the opportunity to add the application. But doing this blindly and without checking the current state can lead to problems. For example, if the user already added the app and an auth_code was passed in, then redirecting back will cause a message to appear to the user - "You already added this app". Our logic on this callback URL does the following )

    ReplyDelete
  28. These are great tips about Facebook API development. A lot of mobile developers will surely find these insights helpful. Thanks for sharing.

    ReplyDelete

Comments on this blog are moderated and we do not accept comments that have links to other websites.

Share