Chapter 3. Public Data APIs

The RESTful data APIs are the core of the Google+ platform. These APIs provide read access to public fields on profiles, activities, and comments. Access to private data, specifically a user’s identity on Google+, is controlled by OAuth 2.0, and API quotas are controlled by API keys.

The easiest way to gain an understanding of the public data APIs is to see them in action.

Baking Disasters is pretty cool, but do you know what would make it cooler? Comments would. Discussion can bring a blog to life, but sadly, they come at a cost. Not only do you need to worry about spam comments, but you also have to expose potentially sensitive software to the outside world.

Fortunately, the Google+ public data APIs expose the comments on your public activities. Since you’re already sharing your blog posts to Google+, you can use JavaScript to render the comments from the activity right in the blog entry. Not only does this prequalify users to reduce spam, but it also allows you to keep Baking Disasters static HTML.

Before you start coding, spend some time to become comfortable with how the APIs behave. The API Explorer from the introduction chapter is perfect for this. You can find it here: https://developers.google.com/apis-explorer/#s/plus/v1/.

Use the API Explorer to trace the same steps that the comments plugin will follow.

  1. Scan through the available methods in the API Explorer. The comments.list method, pictured in Figure 3-1, looks perfect. Unfortunately, it requires an activity ID.

  2. Just above the comments.list method there is an activities.list method, pictured in Figure 3-2. This lists recent activities and provides their IDs.

  3. Listing activities requires one more piece of information: your userId. The easiest way to determine your userId is to copy it from your Google+ profile URL, as shown in Figure 3-3. This technique works for both Google+ pages and user profiles.

  4. Paste your userId into the activities.list form and select the public collection. Click execute to trigger the API call. The JSON response body renders in the history pane at the bottom of the page, as shown in Figure 3-4.

  5. The response consists of some top-level attributes and a collection of activities within the items array. Each entry in the items array has an activity ID, as shown in Figure 3-5. Copy the ID for your most recent entry.

  6. Switch to the comments.list method and supply the activity ID that you just copied, as shown in Figure 3-6.

  7. Execute this request. The history pane now contains the comments associated with that activity, as shown in Figure 3-7.

This flow that you just traced in the API Explorer is a reasonable flow for a comments plugin. Manually discover the activity ID for your activity on Google+, and use JavaScript to fetch and render the comments associated with it.

Google probably provides an official client library for your favorite language. These client libraries make development against APIs faster by taking care of low-level tasks and providing you with an interface that embraces the development style of your language. You’ll save yourself a lot of time by using the official client libraries.

The current library offering is shown in Table 3-1.

The comments plugin will use the JavaScript client library.

Just like the official Google+ plugins, the comments plugin renders comments within a div identified by a special class. It uses JavaScript to replace this placeholder div with the comments for that entry. Example 3-1 shows an example of a div element that the comments plugin will look for:

Create a new JavaScript file named pluscomments.js and include it into each blog entry. Edit the JavaScript file and create a namespace for your functions, and create a variable for your API key, as shown in Example 3-2.

To make calls you need an instance of the JavaScript API client. Load the client library with the name and version of the API and tell it your API key, as shown in Example 3-3. This function is automatically called by the JavaScript client library when it has finished loading and hence must reside in the global namespace.

Next, find all of the g-comment-for divs and extract the activity ID, as shown in Example 3-4.

Take the resulting collection of activity IDs and list the comments for each one. Each API response triggers the same callback function. Example 3-5 shows how to do this.

1

fetchComments is run for each discovered activity ID.

2

Each response calls back to the parseComments function.

3

Rediscover the place to insert the comment, since this information was lost in the chain of callbacks.

4

Construct HTML for all of the comments and insert it into the document.

Finally, source the JavaScript client library and configure it to load the comments plugin. Source it in the entry pages and specify the init function to the onload parameter. Example 3-6 shows the macaron entry header with the client library added: