Google+ API in Java

Posted March 9th, 2012 by

Introduction

The goal of this tutorial is to load publicly accessible profile and activity information from a Google+ user using the Java Google+ clientlibs. You will need to download the clientlibs for Apiary (infrastructure client library) and Google+ client library itself (instructions are provided below).

Getting started

First thing you will need is an API key in order to use the Google+ API. Learn more about API keys and OAuth 2.0 here – http://goo.gl/TPh3w. For the purposes of this tutorial, where we will only access public data, you will just need an API key, which you can get from the API Console here – http://goo.gl/ypZbe. Here are the steps that you have to follow:

Step 1 – Visit the Google APIs Console – http://goo.gl/GPCxH and create a new project.

clip_image001

Step 2 – Make sure to provision the Google+ API.

clip_image003

 

Step 3 – Then view the API key that’s been created for you.

clip_image005

Downloads

You will need the following client libraries in order to get started:

  • Google Apiary Infrastructure client library (google-api-java-client-X.X.X-beta.zip) – http://goo.gl/FqDom
  • Google+ client library (google-api-services-plus-vX-X.X.X-beta.jar) – http://goo.gl/tNOIH (the sources for the client library are included here as well, as well as links to the javadocs).

Using the Google+ API

To learn more about the API, check out this page – http://goo.gl/hmQFm. There are just a new top level operations that are currently possible (as of Jan 27 2012), People, Activities, and Comments. The API is currently read-only, so you won’t be able to write anything to a person’s  (or business page’s) stream, or edit a comment, or create a comment. Here are the javadocs for the Java clientlibs – http://goo.gl/f2apz.

Sample code

public class GetActivities {

  /**
   * userid of the google plus user you want the activity list for
   */
  static String userid = "106242108990073351433"; //FasterLap G+ page (fasterlap.com)


  public static void main(String[] args) throws Exception {

    try {

      GooglePlusService plusService = new GooglePlusService();

      List<Activity> activityList = plusService.getActivityList(userid);

    } catch (HttpResponseException e) {
      System.out.println(e.getResponse().parseAsString());
      System.out.println(Util.extractError(e));
    }


  }


}//end class GetActivities

public class GooglePlusService {

  /**
   * google plus service acess stub
   */
  private Plus plusSvc;

  /**
   * google plus api key
   */
  private String GooglePlusAPIKey = ""; //<-- YOUR KEY GOES HERE

  /**
   * connect to the google plus service
   */
  public GooglePlusService() {

    setupTransport();

  }

  public List<Activity> getActivityList(String userid) throws IOException {

    List<Activity> retval = new ArrayList<Activity>();
    
    Plus.Activities.List listActivities =
        plusSvc.activities().list(userid, "public");

    listActivities.setMaxResults(100L);

    // get the 1st page of activity objects
    ActivityFeed activityFeed = listActivities.execute();

    // unwrap the request and extract the pieces we want
    List<Activity> pageOfActivities = activityFeed.getItems();

    // loop through until we arrive at an empty page
    while (pageOfActivities != null) {
      for (Activity activity : pageOfActivities) {
        retval.add(activity);
        System.out.println("ID " + activity.getId() + " Content: " +
                           activity.getPlusObject().getContent());
      }

      // we will know we are on the last page when the next page token
      // is null (in which case, break).
      if (activityFeed.getNextPageToken() == null) {
        break;
      }

      // prepare to request the next page of activities
      listActivities.setPageToken(activityFeed.getNextPageToken());

      // execute and process the next page request
      activityFeed = listActivities.execute();
      pageOfActivities = activityFeed.getItems();
    }

    return retval;

  }

  /**
   * google plus service stub object
   */
  public Plus getPlusSvc() {
    return plusSvc;
  }

  private void setupTransport() {
    plusSvc = new Plus(new NetHttpTransport(),
                       new GsonFactory());
    plusSvc.setKey(GooglePlusAPIKey);
  }


}//end class GooglePlusService

Output from code

ID z12jjdgomtabifzza04cf1lo0vuadzeickg0k Content: Taking cues from &quot;The Pass&quot; on the Corkscrew at Laguna??

ID z12ci5uwmtquhvff522sgh0aylmjunm4304 Content: HANDS DOWN THE NICEST PORSCHE 997 TURBO S CABRIOLET BUILD I HAVE EVER SEEN. 530HP OF JUST PURE DECADENCE……THIS JUST CAME IN OFF THE TRUCK LAST NIGHT AND IS AVAILABLE.

ID z13dijixetijurded22sgh0aylmjunm4304 Content: Great battle between the Porsche GT1 and the McLaren F1 GTR at Le Mans in 1996. Great footage!


Comments are closed.