Android 101: Working with Google Buzz API

Google Buzz API is a part of the most recent family of Google APIs. This family is based on a new API infrastructure (more about this on this Google IO presentation: How Google builds APIs ). On top of this new infrastructure Google has created a number of tools that help developers build their applications.

As of October 2011, Google announced it would be discontinuing Google Buzz and its API.

HTTP Transport

On Android you have a few options to make the HTTP transport of the messages to/from the Google service you are using (in our case it’s the Buzz service).

  • UrlFetchTransport - Google App Engine only
  • NetHttpTransport - Included in Java and Android SDK. Preferred choice on Android since Gingerbread.
  • ApacheTransport – Included in Android SDK. Preferred choice for older Android SDK’s up to Froyo
  • AndroidHttp.newCompatibleTransport() – Picks NetHttpTransport or ApacheTransport depending on the SDK level.

HTTP connections shouldn’t be performed in the main UI thread - you don’t want to freeze your main activity for an undetermined amount of time.

JSON

JSON has a big focus on the new Google APIs. You should use it as parser/serializer to avoid out-of-memory errors that can be caused by trying to load a JSON response as an object. There are a few choices for the JSON parser/serializer you might want to use.

Authentication

The preferred way to handle authentication and account management in Android is through the usage of the class AccountManager. The AccountManager provides a centralized registry for the user’s online accounts.

The Google APIs provide a more specific way to handle Google accounts through the GoogleAccountManager class.

OAuth

Although not in its final draft yet, OAuth 2.0. is recommended for usage because of the benefits it brings in comparison to its previous version.

  • Supported by (almost) all Google APIs.
  • Finer grained scope of access.
  • Example for Google Buzz:
    • https://www.googleapis.com/auth/buzz for read/write access to Buzz data.
    • https://www.googleapis.com/auth/buzz.readonly for read access to Buzz data.
    • https://www.googleapis.com/auth/photos for read access to Buzz photos.

OAuth tokens are temporary and expire in 1 hour. So it’s important that you check for 401 response errors when making requests and going through the authentication process whenever necessary. Here’s an example taken from a Google IO 2011 presentation (PowerPoint file available here, slide #50):

    @Override
    protected ActivityFeed doInBackground(Void... params) {
     try {
        // execute HTTP requests
     } catch (HttpResponseException e) {
        if (e.response.statusCode == 401) {
          accountManager.invalidateAuthToken(accessProtectedResource.getAccessToken());
          accessProtectedResource.setAccessToken(null);
          SharedPreferences.Editor editor2 = settings.edit();
          editor2.remove(PREF_AUTH_TOKEN);
          editor2.commit();
          getAuthToken(account);
        }
     }

Here you can find a document by Google on using OAuth 2.0 to access Google APIs.

API Access Key

In addition to handling accounts you’ll also need an access key provided by Google to access Google users’ contents through APIs. Here’s an example with a dummy key:

buzz = new Buzz(new NetHttpTransport(), accessProtectedResource, new AndroidJsonFactory());
buzz.accessKey = "ABCdef123_9q";

You can get one of these access keys through the Google APIs Console.

Here you can find instructions on how to create an access key.

Read more about access keys here.

Google Buzz API

The Google APIs Explorer is a super useful tool to test the JSON returned by each call. Although you don’t need to use the JSON directly you can use the explorer to figure out the names of the fields and which field returns what.

There’s a Google IO presentation, Best Practices for Accessing Google APIs on Android, that gives you some nice pointers about the use of Google APIs and Yaniv Inbar, the presenter, uses code examples of how to fetch Google Buzz data. You can find the full working code of the example here (buzz-v1-json-oauth2-android-sample). This code was actually our starting point for our own app.

Limitations

The Google Buzz API Java Client 1.0.0 beta that we’ve used in our app is not 100% implemented, so you need to fetch some of the JSON attributes in an almost raw format. See here is how I had to fetch the attachment’s links from the buzz object:

ArrayList< ArrayMap<String, Object> > o = (ArrayList<ArrayMap<String, Object>>) a.links.unknownFields.get(linkType);
String s = (String) o.get(0).get("href");

Also, posts in your stream that aren’t public will show up with this message:

Private post only viewable at https://plus.google.com/xxx/posts/xxx

Dércia Silva
Posted by Dércia Silva on November 16, 2011

Related articles