Skip to content

I wrote a Google+ Library

February 3, 2012

Because of a project, I have been trying out several Google+ Libraries. The following libraries were the ones I tried:

The first being made by Google and the second by… Not Google.

I cannot say I was satisfied with any of those two. The first one required what seemed like a thousand dependencies while the second one refused to work due to a JSON exception that seemed to have no documentation on-line. So, I decided I’d do myself a favor and make something that works the way I want it to work: with as few dependencies as possible and very easy to use.

I used three dependencies:

I picked these three (well, two actually, commons-codec was required by scribe) because they are easy in use and do not require other dependencies.

And this is the result: My very own Google+ library

I designed it with a few guidelines in mind:

 1. Easy to get it working

GooglePlus gPlus = new GooglePlus(apiKey, secret);
 Desktop.getDesktop().browse(new URI(gPlus.getAuthorizationUrl()));
 gPlus.verifyUser(new Scanner(System.in).next()); 

Three lines and we’re connected to the G+ api.

 2. Cope with calls that weren’t predefined

 private static final String apiUrl = "https://www.googleapis.com/plus/v1/";

public Response getResponse(String apiCall) {
 OAuthRequest request = new OAuthRequest(Verb.GET, apiUrl + apiCall);
 oAuthService.signRequest(accessToken, request);
 Response response = request.send();
 return response;
}

The library provides predefined calls to the api, such as “findUser(String query)”. Normally the inner workings would be shielded from external use, but if you need to make your own calls to G+, you can call the above method in net.mymilkedeek.social.gplus.core.Manager with your custom api call.

This will come in handy if Google updates its G+ api and this library doesn’t get updated. No need to wait, just put in the new api calls and process the Json response.

3. Easy to expand

This is awesomely achieved. To add the functionality for Comments I needed to:

  1. Create a Comment class (with fields)
  2. Create CommentManager (extends Manager<Comment>)
  3. Create CommentJsonParser (implement JsonParser<Comment>)
  4. Parse the Json to the Comment object
  5. Write one line functions in the Manager

The to-do’s

  • Add optional parameters.
  • Write more comments.
  • Better exception handling.

Download it, browse the source code, don’t download it. Do with it as you please. As long as you report bugs/notify me of things that could have been better.

  Some Example

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Scanner;

import net.mymilkedeek.social.gplus.GooglePlus;
import net.mymilkedeek.social.gplus.activity.Activity;
import net.mymilkedeek.social.gplus.activity.ActivityManager;
import net.mymilkedeek.social.gplus.comment.Comment;
import net.mymilkedeek.social.gplus.comment.CommentManager;
import net.mymilkedeek.social.gplus.person.Person;
import net.mymilkedeek.social.gplus.person.PersonManager;

import org.json.JSONException;

public class Main {

private static final String apiKey = "XXX";
private static final String secret = "XXX";

 public static void main(String[] args) throws JSONException, IOException,
 URISyntaxException {
 GooglePlus gPlus = new GooglePlus(apiKey, secret);
 Desktop.getDesktop().browse(new URI(gPlus.getAuthorizationUrl()));
 gPlus.verifyUser(new Scanner(System.in).next());

 ActivityManager activityManager = gPlus.getActivityManager();
 List<Activity> activities = activityManager
.getActivitiesFromUser("111486046861792612952"); // that's me!

 for (Activity a : activities) {
 System.out.println(a.getId());
 PersonManager personManager = gPlus.getPersonManager();
 List<Person> persons = personManager.findByActivityId(a.getId(),
 PersonManager.PLUSONERS);

 for (Person p : persons) {
 System.out.println(p);
 }

 persons = personManager.findByActivityId(a.getId(),
 PersonManager.RESHARERS);

 for (Person p : persons) {
 System.out.println(p);
 }
 }
 }
}

This piece of code gets all of my activities (or posts) and then gets all the users who +1’d my post or shared it. Easy peasy.

From → java, programming

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: