skip to main | skip to sidebar

Getting Started with Google Plus API

After doing my latest jQuery plugin for Google+, I would like to share a few tips to help you get started quickly with Google+ API.

Optimizing API Queries

There are some common parameters in the API methods, 2 of them are very important:
  1. prettyPrint, default is "true" to return formatted JSON for better readability and debugging. Otherwise, you should set this to "false" to have unnecessary whitespace removed and reduce the output size.
    * Currently it works only if you use it with all letters in lower case "prettyprint", however that might be temporary issue.
  2. fields, a selector to specify which fields to include in a partial response instead of returning many unused data fields. Doing that yourself can be confusing, therefore Google+ API has a Field Editor to help you select fields in a hierarchical view.
    * In each API method page, click on "try it" link , then click "open fields editor" to popup the editor. If you see nothing under "try it" then you need to be logged in the API area, Or logged in with your Google+ enabled account.
Using both options can shrink the response size to 50%..

Image Resizing

Querying person profile image can return huge images based on what the user has uploaded for his profile image. To overcome that, you can resize it by appending the query string "?sz=x" where x is the needed width and height.

Also, querying activities list returns attachments thumbnails with height resized to 100px using Google Page Speed Proxy like this:
http://images0-focus-opensocial.googleusercontent.com/gadgets/proxy?container=focus&gadget=a
&resize_h=100
&url=http%3A%2F%2Flh3.googleusercontent.com%2F-72Og6OuLdzQ%2FTnSfagloy1I%2FAAAAAAAAATE%2Fl_ZPaXjjrfc%2Fs800%2FGoogle-Plus-Activity-Widget.jpg

From the previous thumbnail URL, you can see that parameter "resize_h" is used to resize by image height.Having said that, you can set the height as needed using that parameter. Also, there is "resize_w" to resize the image by width.
* Note that, resizing is done with aspect ratio preserved. additionally, you shouldn't use both parameters for non-square images as that will distort it.

Handling API Errors

Fortunately, Google+ API returns error messages in a JSON format with a HTTP status of "200 ok". And that is great for Javascript apps, since other failure statuses like "500 Internal Server Error" or "404 Not Found" would prevent JSONP callbacks from being fired.

This is a sample error response for an invalid user ID:
{
   "error":
   {
      "errors": [
         {
            "domain":"global"
            ,"reason":"invalidParameter"
            ,"message":"Invalid value '123x'. Values must match the following regular expression: 'me|[0-9]+'"
            ,"locationType":"parameter"
            ,"location":"userId"
         }
      ]
      ,"code":400
      ,"message":"Invalid value '123x'. Values must match the following regular expression: 'me|[0-9]+'"
   }
}

* However, There is 1 error that API can't handle well which is passing empty user ID.

Getting Activity Title

When working with activities list, you might find that posts titles are empty. And that happens when user shares a photo or an article without adding any description.

In that case, you need to loop the attachments array in that post, then get any non-empty "displayName" from attachment items.
* displayName could be a photo caption or an article title.

Parsing Dates

Google+ API uses RFC3339 format for dates like published date of an activity. Although you can parse these formats with "Date.parse" in Firefox / Chrome / Opera, it will not work for IE / Safari.

So, We need to borrow this function from Topper to parse the date correctly in all browsers.

// define new method for dates
Date.prototype.setRFC3339 = function(dString){
   var utcOffset, offsetSplitChar;
   var offsetMultiplier = 1;
   var dateTime = dString.split('T');
   var date = dateTime[0].split('-');
   var time = dateTime[1].split(':');
   var offsetField = time[time.length - 1];
   var offsetString;
   offsetFieldIdentifier = offsetField.charAt(offsetField.length - 1);
   if (offsetFieldIdentifier == 'Z') {
      utcOffset = 0;
      time[time.length - 1] = offsetField.substr(0, offsetField.length - 2);
   } else {
      if (offsetField[offsetField.length - 1].indexOf('+') != -1) {
         offsetSplitChar = '+';
         offsetMultiplier = 1;
      } else {
         offsetSplitChar = '-';
         offsetMultiplier = -1;
      }
      offsetString = offsetField.split(offsetSplitChar);
      time[time.length - 1] == offsetString[0];
      offsetString = offsetString[1].split(':');
      utcOffset = (offsetString[0] * 60) + offsetString[1];
      utcOffset = utcOffset * 60 * 1000;
   }
   this.setTime(Date.UTC(date[0], date[1] - 1, date[2], time[0], time[1], time[2]) + (utcOffset * offsetMultiplier ));
   return this;
};

// sample call
var d = new Date();
d.setRFC3339( '2011-09-15T18:28:43.000Z' )
alert(d);

Getting API Key

Obviously without an API key you can't do any thing with the API, So you need one:
  1. Go to https://code.google.com/apis/console#access.
  2. Login and click "Create Project".
  3. Under service list, click the "off" button next to "Google+ API" to turn it on.
  4. Accept the agreement and click "next".
  5. From the left menu, Go to "API Access" and copy your API key.

API key rate limits are 1k queries/day. And you can set per-user per-second limits on the Quotas pane to prevent users from using up your API quota. To modify that value, click the "Set per-user limits" button and adjust the rate as desired. You may also allow requests from some hosts or IP addresses only. To enable that, click on " Configure traffic filters" on Quotas page.

Another great feature, is that Google APIs Console provides monitoring panes to view your API traffic information.

Hope these tips were helpful to you, did you find other tips about Google+ API?

1 comments

  1. Mike Haog // September 6, 2013 at 7:34:00 PM GMT+10  

    wow!! been searching for a week to figure out how to get my api key. I was beginning to think it fell into the category of a Jackalope. Thank you for the easy step by step process.

Post a Comment

Thank you for taking the time to comment..
* If you have a tech issue with one of my plugins, you may email me on mike[at]moretechtips.net

Mike

Mike MoreWeb developer, jQuery plugin author, social media fan and Technology Blogger.
My favorite topics are: jQuery , Javascript , ASP.Net , Twitter , Google..
<connect with="me"> </connect>

Subscribe by email

Enter your email address:

or via RSS