Google+ read API: activities and comments

In this post,  I will look into how you can use the Google+ read API from JavaScript to retrieve activities and comments from a profile. I was interested in this because I believe it would be possible to make a commenting system similar to Disqus that is built entirely on top of the Google+ platform. At a minimum, it would be interesting and even useful to pull dialog from G+ into my blog.

This is exciting because it really demonstrates the power of Google+ as a platform: entire products could potentially be developed against and powered by the Google+ API. I will share what I have done so far and will conclude with a few comments on the limitations of the API.

Retrieving comments for all activities

In order to retrieve the comments for an activity, you must first retrieve the activityID. To do this, you need to use the activities List API. The following JavaScript code will retrieve all of the activities for a particular profile ID:

    // Gets the activities for a profile
    function getActivities(profileID){
      var activities = null;      
      var URL        = "https://www.googleapis.com/plus/v1/people/" + profileID + "/activities/public?alt=json&key=" + key;
      var request = new XMLHttpRequest();
      request.open('GET', URL, false);
      request.send(); // because of "false" above, will block until the request is done 
                      // and status is available. Not recommended, however it works for simple cases.

      if (request.status === 200) {
        if (debug) console.log("retrieved activities \n\n");
        var activities = jQuery.parseJSON(request.responseText).items;
        console.log("Discovered " + activities.length + " activities");
      }else{
        handleRequestIssue(request);
      }

      return activities;
    }

After retrieving the activities, you can extract the identifier for the activity. To do this, I traverse the list of activities that are returned from the call to the profile activity read. The activity id attribute will later be used to retrieve the comments from a particular activity. The following code shows the traversal of the activities and debugging in the console:

  for (var i=0; i < activities.length; i++) {
          console.log("trying to do something with an activity: " + i);
          var activity = activities[i];

          console.log(activity.id);
  }

After retrieving the activity, you can then perform a query against the comment API.  The following code shows how this is done in JavaScript:

    function getCommentsForActivity(activityID){
      var comments = "";      
      var URL        = "https://www.googleapis.com/plus/v1/activities/" + activityID + "/comments?alt=json&key=" + key;
      var request = new XMLHttpRequest();
      request.open('GET', URL, false);
      request.send(); // because of "false" above, will block until the request is done 
                      // and status is available. Not recommended, however it works for simple cases.

      if (request.status === 200) {
        if (debug) console.log(request.responseText);
        var comments  = jQuery.parseJSON(request.responseText).items;

        if (debug){
          for (comment in comments){
            console.log(comment);
          }
        } 

      }else{
        handleRequestIssue(request);
      }

      return comments;
    }

    function manualTrigger(){
      var activities = getActivities("109716647623830091721");
    }

The following code brings this all together and pulls activities and comments from my Google+ profile to render some trivial HTML in a div using some jQuery.

      $(document).ready(function () {

        var renderMe = "";
        var activities = getActivities("109716647623830091721");

        console.log("activities retrieved: " + activities.length);

        for (var i=0; i < activities.length; i++) {
          console.log("trying to do something with an activity: " + i);
          var activity = activities[i];

          renderMe += "<br/><div class=\"article\"><p>" + activity.title + "</p>";
          console.log(activity.id);

          // get comments
          var comments = getCommentsForActivity(activity.id);
          for (var j=0; j<comments.length; j++){
            renderMe += "<br/><div class=\"comment\">" + comments[j].object.content + "</div>";
          }
          renderMe += "</div>";
        }
        console.log("I'm done");

        document.getElementById("ac").innerHTML = renderMe;
      });

It’s not the prettiest code or demonstration, but you can see all the steps working together on this demonstration of Google+ activity and comments reading.

Remarks, limitations, conclusions

First and foremost, Google+ restricts the activities that can be retrieved to posts that are Public. Posts shared with circles cannot be read. This is by design to prevent unintended shares with people outside of a circle. Also of note, Google+ has real identities tied to accounts so it is very important to be privacy conscious.

Next, the Google+ platform has made a conscious decision to block stream write APIs, this means that commenting directly to Google+ is not possible from the blog view. Similar to the circle read API this protects a user’s stream from unintended shares and keeps others streams vibrant with high quality activity.

Others have done some implementations of WordPress posting and commenting using the Google+ API. While I was writing the post and experimenting with the API, my coworker mentioned that a member of the Google+ product team, Will Norris, had already created an excellent WordPress plugin that lets you create Google+ posts tied to a blog. Another member from my team created a static commenting system on her blog as well.  Check the See Also links for more information.

See Also