'. '


From APIDesign

Jump to: navigation, search

While ago I got a question from a curious to-become-reader of TheAPIBook whether, after reading it, he will be able to create API for a Web OS like Facebook. I had to admit that my book could help only indirectly. Some principles apply, but the main focus is on libraries, while Facebook is more about REST APIs. Anyway, modernity cannot be prevented, so I am slowly deep diving into this slightly different world too.


Facebook Desktop Application

I can't help myself, as a founder of NetBeans I still feel like a desktop guy. As a result I wanted to access Facebook from my desktop application. Actually, I'd even wanted to create an application that can run headless, on a server. I wanted to create a RSS feed publisher. An application that can read my blog entries and post them up on my facebook page.

This is not easy. Desktop applications are not the primary target of facebook and it is hard to find good tutorials, even harder to find some that are modestly up to date. However I made it work. If you want to make your application work too, here is the howto. I am sure it is not perfect, however please note that this is a wiki page. Everyone can make it better. If you know better way to do some of the tasks, feel free to update the page.


The first step is to register at http://www.facebook.com/developers/createapp.php and obtain your application's key and secret. It does not matter much what values you put in the registration form. None are important as far as I can tell.


Having your key and secret you can start writing your application. Sources of mine feedbook are available in a Hg repository at http://source.apidesign.org/hg/feedbook/, you can use:

$ hg clone http://source.apidesign.org/hg/feedbook/
$ cd feedbook
$ mvn install

to get them and compile them. First thing to do is to login and obtain a session ID:

Code from Main.java:
See the whole file.

private static String login() 
throws IOException, FacebookException, URISyntaxException {
    FacebookJaxbRestClient login = new FacebookJaxbRestClient(
        APP_KEY, APP_SEC
    final String token = login.auth_createToken();
    final URI u = new URI("http://www.facebook.com/login.php?"
            + "api_key=" + APP_KEY + "&auth_token=" + token);
    String msg = "authentication";
    browser(msg, u);
    return login.auth_getSession(token, false); 

Alas, I don't know how to login using pure REST APIs. As such I have to open a browser and let the user login manually. I'd rather prefer to login in a headless mode, but seeking the Internet did not help me at all to find a way to do it.


To post on your wall, your application needs a permission. You can use REST API to find out whether you have a permission or not. If you however want the user to grant you additional rights, the simplest thing is to open a browser once again:

Code from Main.java:
See the whole file.

while (!fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
    URI u = new URI("https://graph.facebook.com/oauth/authorize?"
            + "client_id=" + APP_KEY
            + "&redirect_uri=http://www.facebook.com/connect/login_success.html"
            + "&scope=publish_stream");
    browser("Need permission to publish links", u);

With appropriate permissions it is then piece of case to post anything on your wall:

Code from Main.java:
See the whole file.

    "link: %s date: %s title: %s\n", 
    item.getLink(), item.getPubDate(), item.getTitle()
Long ok = fb.links_post(user, item.getLink(), item.getTitle());
System.out.println("posted as " + ok);

The code is not complex, but it took me few days to find all the glitches and eliminate paths that lead nowhere. If I could eliminate the need to open browser, I think my RSS Feedbook application would be perfect.

Publish Your RSS Too!

If you are interested, you can use the same application to publish your blogs too. In contrast with common server side facebook applications, you don't need to share with me any secrets, you don't need to publish any keys, grant any permissions to applications you don't control. Just:

  1. Download Feedbook-X.Y.jar from http://hudson.apidesign.org/hudson/job/feedbook or compile the sources yourself
  2. Register new personal application at http://www.facebook.com/developers/createapp.php (you can choose any values you want, probably)
  3. Run Feedbook with the obtained application key, secret and pointer to your blog feed:
$ java -jar Feedbook-X.Y.jar app_key app_secret feed_url

Work with the opened browsers, press enter to move the application forward. All your blogs will be written to your wall. Next time you want to publish new blogs, just run:

$ java -jar Feedbook-X.Y.jar

it will use the same parameters and before and publish only newer, not yet published blogs. Enjoy Facebook from desktop!


Personal tools