Wednesday, September 21, 2011

Google APIs Client Library for Java developers

Google APIs Client Libraries & Tools team making available a Beta version of the open source Google HTTP Client Library for Java. This is the common HTTP client library. 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.

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:

// 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());

Source: Google code

No comments:

Post a Comment