My favorites | English | Sign in

Google Health Data API

Developer's Guide: Java 1.4.2

Important: This is an old version of this page. For the latest version, use the links in the left-side navbar.

The Google Health Data API allows client applications to view and update Health content in the form of Google Data API feeds. Your client application can request a user's profile as a CCR document, retrieve a list of notices sent to their account, or send a notice to a user.

In addition to providing some background on the capabilities of the Health Data API, this document provides examples for interacting with the API using Java 1.4.2 and the Mt Tabor Google Health Java client library. The Mt Tabor Google Health Java client library is intended for clients running Java 1.4.2 who cannot use the standard Google Data API Java libraries. If you're interested in understanding more about the underlying protocol used to interact with the Health Data API, please see the Protocol section of this developer's guide.

If you are running Java 1.5, see the Google Health Java 1.5 documentation.

Contents

Audience

This document is intended for programmers using Java 1.4.2 who want to write client applications that can interact with Google Health. The following instructions assume some knowledge of building, deploying, and running Java web applications.

Getting started

To use the Mt Tabor Google Health Java client library, you'll need the Java 2 Standard Edition 1.4.2 (J2SE 1.4.2) or an application server that uses Java 1.4.2. After downloading the Mt Tabor Google Health Java client library, you'll find the JAR you need to get started in the target subdirectory of the distribution. Follow the instructions in INSTALL.txt to ensure you have the library's dependencies.

To compile the examples in this document into your own code, you'll need to import the following packages:

import com.mttaboros.health.authsub.AuthSubUtil;
import com.mttaboros.health.feed.FeedClient;
import com.mttaboros.util.IOUtils;
import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import com.sun.syndication.feed.atom.Person;
import org.apache.commons.lang.StringEscapeUtils;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

Back to top

Authenticating to the Health service

Before you can interact with Google Health, you must register your application with Google. Register your domains by reviewing and fill-out the API Terms of Service.

AuthSub Authentication

The AuthSub login protocol allows your web-based application to have a user authenticate through Google's servers. For information on the basics of using AuthSub with the Google Data API client library (for Java 1.5+), see Using AuthSub with the Google Data API Client Libraries.

However, there are some differences when using the Mt Tabor Google Health Java client library, which are described below.

Generating an AuthSub URL for Google Health

String nextUrl = "http://www.example.com/health.jsp"; // Page on your site where users will be redirected after authorization.
String scope = "https://www.google.com/health/feeds/";

HashMap extraParams = new HashMap();
extraParams.put("permission", "1");

String authSubLink = AuthSubUtil.getRequestUrl("https", "www.google.com", "/health/authsub",
    nextUrl, scope, false, true, extraParams);

The reason for passing "/health/authsub" to getRequestUrl is that Health uses a special AuthSub handler, and this value will replace the standard AuthSub handler of "/accounts/AuthSubRequest".

Back to top

Get a user's profile

The profile feed allows you to retrieve a CCR document describing a user's health information stored with Google. Each profile is encapsulated inside of an Atom entry. The Mt Tabor Google Health Java client library can be used to extract the CCR information and print it to the page:

Feed atomFeed = FeedClient.getAtomFeed(authSubSessionToken,
  "https://www.google.com/health/feeds/profile/default?digest=true", null);
String output = "";
Iterator iterator = atomFeed.getEntries().iterator();
while (iterator.hasNext()) {
  Entry entry = (Entry) iterator.next();
  Iterator foreignMarkupIterator = ((List) entry.getForeignMarkup()).iterator();
  while (foreignMarkupIterator.hasNext()) {
    Element element = (Element) foreignMarkupIterator.next();
    if ("ContinuityOfCareRecord".equals(element.getName())) {
      output += "Contains CCR data:\n";
      output += "<pre>\n";
      XMLOutputter xmlOutputter = new XMLOutputter();
      xmlOutputter.setFormat(Format.getPrettyFormat());
      StringWriter writer = new StringWriter();
      xmlOutputter.output(element, writer);
      output += StringEscapeUtils.escapeHtml(writer.getBuffer().toString());
      output += "<pre>\n";
    }
  }
  output += "</div><br/>\n";
}
response.getWriter().write(output);

Back to top

Interacting with the register feed

Notices are the primary means by which your application can communicate and interact with the user. They allow you to send informational messages and CCR profile data to a user. Currently, the only thing you can do using AuthSub is post new notices; you can't update or delete notices using AuthSub. ClientLogin allows for reading existing notices, posting new notices, and updating or deleting notices.

Get a list of notices

Note: This feature is available only with ClientLogin.

You can retrieve notices that your application has access to view using the register feed in conjunction with a profile ID: register/ui/profileID. Note that you can use standard Google Data API query parameters to narrow down the results of this query.

The following code retrieves all of the notices associated with the given profile. It also prints out any attached CCR data appended to a notice.

Feed atomFeed = FeedClient.getAtomFeed(authSubSessionToken, "https://www.google.com/health/feeds/register/ui/profileID", null);
String output = "";
Iterator iterator = atomFeed.getEntries().iterator();
while (iterator.hasNext()) {
  Entry entry = (Entry) iterator.next();
  output += "<div>\n";
  output += "From: " + ((Person) entry.getAuthors().toArray()[0]).getName() + "<br/>\n";
  output += "Date: " + entry.getPublished() + "<br/>\n";
  output += "Subject: " + entry.getTitle() + "<br/>\n";
  output += "Message: " + ((entry.getContents().isEmpty())
    ? "empty" : ((Content) entry.getContents().toArray()[0]).getValue()) + "<br/>\n";
  Iterator foreignMarkupIterator = ((List) entry.getForeignMarkup()).iterator();
  while (foreignMarkupIterator.hasNext()) {
    Element element = (Element) foreignMarkupIterator.next();
    if ("ContinuityOfCareRecord".equals(element.getName())) {
      output += "Contains CCR data:\n";
      output += "<pre>\n";
      XMLOutputter xmlOutputter = new XMLOutputter();
      xmlOutputter.setFormat(Format.getPrettyFormat());
      StringWriter writer = new StringWriter();
      xmlOutputter.output(element, writer);
      output += StringEscapeUtils.escapeHtml(writer.getBuffer().toString());
      output += "</pre>\n";
    }
  }
  output += "</div><br/>\n";
}
response.getWriter().write(output);

Send a notice

Creating a new notice is accomplished by constructing an Atom entry and then inserting it into the register feed. This process is implemented within the FeedClient provided in the Mt Tabor Google Health Java client library.

The below code shows adding a CCR document from a string variable called ccrXml.

FeedClient.addEntry(authSubSessionToken, "https://www.google.com/health/feeds/register/default", "An example title", ccrXml);

Back to top