Monday, September 19, 2011

Google APIs Client Library for Java: now with OAuth 2.0


By Yaniv Inbar, Google APIs Client Team

During Google I/O 2011, we announced a major milestone by releasing the Beta version of the open source Google APIs Client Library for Java. This release included service-specific libraries and samples for Google APIs, built on our new client library generation infrastructure. Since that version 1.4 launch, we’ve been comfortable enough with the stability and features of the library that we want you to start building real production Java 5, Android, and Google App Engine applications and send us your feedback.

Today we are announcing a new milestone for the Java client library. With the version 1.5 release, we’re making available the open source Google OAuth Client Library for Java in Beta, with support for both OAuth 1.0a and OAuth 2.0. OAuth is an open standard for allowing a client application to securely gain access to a user’s private data stored on Google without ever asking for their password. Most Google APIs support OAuth 2.0, and we want to encourage adoption of OAuth 2.0 more widely on the web. That’s why we built this library to work with any API on the web -- not just Google APIs -- that comply with the OAuth specifications. Our current implementation of OAuth 2.0 is based on draft 10, but we will update it soon to the final draft, once it becomes an official standard. We encourage you to try it and send us your feedback.

Here is an example of how easy it is to use the OAuth 2.0 library to make a request using the library for the Google+ API (check out more samples):
// Set up the HTTP transport and JSON factory
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();

// Set up OAuth 2.0 access of protected resources 
// using the refresh and access tokens, automatically 
// refreshing the access token when it expires
GoogleAccessProtectedResource requestInitializer = 
    new GoogleAccessProtectedResource(accessToken, httpTransport, 
    jsonFactory, clientId, clientSecret, refreshToken);

// Set up the main Google+ class
Plus plus = new Plus(httpTransport, requestInitializer, jsonFactory);

// Make a request to access your profile and display it to console
Person profile = plus.people().get("me").execute();
System.out.println("ID: " + profile.getId());
System.out.println("Name: " + profile.getDisplayName());
System.out.println("Image URL: " + profile.getImage().getUrl());
System.out.println("Profile URL: " + profile.getUrl());
Finally, we are making available a Beta version of the open source Google HTTP Client Library for Java. This is the common HTTP client library that the above two libraries are built on, and is built to work with any API on the web. It features a pluggable HTTP transport abstraction that allows it to work seamlessly on any of the supported Java platforms, support for efficient JSON and XML data models for parsing and serialization, and a pluggable JSON and XML parser so you can use whatever works best for you. Please try it and send us your feedback.

We are looking forward to finding out what you can build using these libraries on Google APIs. Please let us know how we can make the libraries easier to use and better suited for your needs.

As we announced at Google I/O 2010, we've been developing APIs that can provide descriptions of themselves via metadata. This new technique makes it easier to create and maintain client libraries that support more languages, work with more APIs, and are easier to use than ever before. This post announces one of several recent major milestones for our client libraries.


Yaniv Inbar is a Senior Software Engineer and Technical Lead of the Google APIs Client Libraries & Tools team. He is the lead developer of the open source Google APIs Client Library for Java. Yaniv has worked at Google for 5 years, and has a total of 12 years industry experience as a software engineer.

Posted by Scott Knaster, Editor

3 comments:

  1. Do you have these available in a public Repo? It would be nice to have the POM file dependency entries.

    Thanks

    Vinay (reversed ...)

    ReplyDelete
  2. My goodness, This is the best thing you've done !!

    I've been waiting for this for the past few months, the OAuth helper libraries were really indispensable !

    Thanks again ! :-)

    ReplyDelete
  3. Anybody got any ideas why I'm getting class not found errors when trying to use com.google.api.services.plus, like for the example above?

    I'm using the google-api-services-plus-v1-1.3.0-beta.jar

    ...Which is included in the google-api-java-client-1.6.0-beta.zip. All the other classes from that zip seem to build ok.

    My project is a non Maven project btw.


    Uncaught exception from servlet
    java.lang.NoClassDefFoundError: com/google/api/services/plus/Plus

    Maybe I'm missing some dependencies? Any suggestions greatly appreciated!

    ReplyDelete