Saturday, July 10, 2010

Integrating OACurl With A Java Desktop Application

In my previous post, I introduced the Google Buzz API and the technologies surrounding it. In this post, I will be emphasizing on how to integrate OACurl with a Java Desktop Application to make the authentication process simpler. Before I continue, let me categorical state that this method is good for small student projects; but for serious production applications, OACurl integration isn't a good option.

OACurl is primarily an OAuth aware wrapper around the popular cURL command line utility. If you are wondering what cURL is, it is a software project providing a library and command-line tool for transferring data using various protocols like HTTP, FTP, HTTPS, LDAP, IMAP, POP3, etc. The beauty of cURL is that it simplifies the process of data transfer using these protocols to a very large extent. OACurl is an extension to this utility which supports OAuth, the authentication/authorization protocol of the Google Buzz API. OACurl provides an easy way to experiment with the Buzz API.

Before I start with OACurl Integration, let's see how we can connect a desktop application to the Google Buzz API without OACurl. As I mentioned in the previous post, the OAuth token request and authorization process takes place through a Web Browser. So the desktop application should open the Browser and allow Google to verify the user credentials. Then Google issues an authorized token to a Callback URL as specified by the application. This has to be detected by the application and it has to regain focus. For a more detailed view on this, check out - http://code.google.com/apis/accounts/docs/OAuthForInstalledApps.html.

The biggest challenge in this process is that in devices like Gaming Consoles, Setup Boxes, etc., where a Web Browser isn't available the entire procedure goes haywire. Google is working on a solution for this, until then we have to adjust with the Web Browser technique.

OACurl is a pretty easy tool to work with. It is available as a Java Archive with multiple classes for various operations like Logging in, Fetching data from URLs, etc. To use OACurl directly from the command-line, refer http://code.google.com/apis/buzz/v1/oacurl.html.

To use OACurl within a Java Desktop Application, add a reference to the jar file from your project. Now there are two classes which do the entire thing for us - Fetch and Login. Both of them contain a main method; actually it is these classes that are being used in the URL mentioned above. But instead of using classes from the command-line, we will directly call the main method in them from our Java program.

A bit of reverse engineering makes it clear that the flags like "-buzz", "-X", "POST", etc. are being sent as command line arguments. So we can create a String array of these flags and sent it to the main method.

com.google.oacurl.Login.main (new String[] { "-buzz" });
com.google.oacurl.Fetch.main (new String[] { "-X", "POST", URL });

The main methods push the output content to the default output stream as specified by System.out. By default, this is the user screen. If we want to use this output, we can transfer this output into a temporary file and read from this file.

File temp = File.createTempFile ("output", null);
PrintStream output = new PrintStream (temp);
System.setOut (output);

Similarly, the main methods read the input from the default input stream as specified by System.in. By default, this is the keyboard. If we have to send our input, we can create a temporary file, write the content into it and make the file the default input stream.

File temp = File.createTempFile ("output", null);
// Write into the file
PrintWriter writer = new PrintWriter (temp);
System.setIn (writer);

This way we can use the Google Buzz API within our Java Desktop Application without having to worry anything about the authorization process. Developers curious to know how this works can explore the OACurl project hosted on Google Code at http://code.google.com/p/oacurl/. The complete source code of the Login and Fetch classes as well as the Callback Server and other classes is available at the link.

Before I wind up, here is a small project developed in NetBeans which reads the latest buzz and publishes buzz content.

3 comments:

Sandip Dev said...

good job dude...nice post...

navya said...

I have read your blog its very attractive and impressive. I like it your blog.

.Net Training in Chennai | .Net Online Training | Dot Net Training in Chennai

Dot Net Online Training | LINQ Online Training

Susan said...

Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me. The 192.168.l.254 are used by router brands such as Westell ADSL Modems, 3Com routers, Linksys switches, 2Wire routers, TP-Link routers Alcatel ADSL Modems, Thompson ADSL routers, ADSL billions routers, SRW2023 and others.