I had previously made a module for a PHP website that cross-posts all posts to a facebook Page, so I knew a little about the facebook API and the whole ecosystem. This time I wanted to use Java, since it’s currently my primary programming language. I didn’t want to reinvent the wheel, so I started looking around for a Java API. Facebook doesn’t maintain such an API anymore, but I found two that could work.
http://code.google.com/p/facebook-java-api/ was the first one I tried, but it seemed to employ the old api key/secret key/session key approach which seems to have been replaced by a token key approach by facebook. This prompted me to test the other option which was http://restfb.com/. It seemed like a clean OO like solution, so I started to experiment with it.
My idea was to query facebook to see which of my friends were online. To accomplice that I registered a pseudo application on facebook’s developer site https://developers.facebook.com/apps and used the Graph API Explorer https://developers.facebook.com/tools/explorer/ to get a valid access token for my app that had the required access rights to query what I needed. The required rights were user_online_presence, friends_online_presence and offline_access. (offline_access may be deprecated in the near future and thus the access token may have to be refreshed)
I decided to make a simple command line runnable JAR instead of a web application, but at the same time create code that could be used in a web application later on. For the arguments I chose Apache Commons CLI, which creates usage print outs from arguments automatically. For logging I chose Log4J, but incorporated SLF4J to get logging from RestFB into my own logs.
Basically all that was needed to get the data from facebook was to create an instance of DefaultFacebookClient and query using facebook’s own FQL language, which is similar to SQL. Since RestFB is very OO I needed to create an object to hold the user’s uid, name and online presence. I started by querying all the rows, even the ones that were offline, but soon changed it to query only the active and idle statuses. The queries involved were:
Friends:
SELECT uid, name, online_presence FROM user WHERE online_presence IN ('active', 'idle') AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())Myself:
SELECT uid, name, online_presence FROM user WHERE online_presence IN ('active', 'idle') AND uid = me()I did create cool stuff around that, but this is all you need to know. 🙂



0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.