English | Site Directory

Google Base Data API

Java Raw Protocol - QueryExample3

QueryExample3 introduces authentication. It also shows how to query your own items using the items feed.

The previous two examples demonstrated how to query the Google Base data API public feeds, also known as the Snippets feed . Snippets are accessible to anyone without authentication. The downside is that snippets are the "indexable" version of the items and can slightly differ from the original items. It can also take a while for a newly inserted or updated item to show up in the snippets feed. Therefore, the Google Base data API server exposes the customer-specific "items" feed so that you can change your own items. This feed is very similar to the snippets feed, except that:

  • it only allows you to query your own items, and thus you need to authenticate in order to access it
  • there is no delay between updating an item and being able to display it in the items feed

Refer to the Customer Items feed reference for more information about the items feed.

You will need a Google Account email and password in order to run this example.

QueryExample3 connects to your items feed and dumps your items to the console, just as in QueryExample1. If you don't have any items in Google Base yet, use InsertExample to insert one, or go to the Google Base Provider Frontend and insert one.

Contents

  1. Running QueryExample3
  2. Stepping through the QueryExample3 code

Running QueryExample3

  1. Obtain a developer key for an "installed application".
  2. Edit QueryExample3.java and fill in the DEVELOPER_KEY static string with your developer key.
  3. private static final String DEVELOPER_KEY = "";
  4. Enter your Google Accounts email address and your password in the EMAIL and PASSWORD static strings:
  5. private static final String EMAIL = "";
    private static final String PASSWORD = "";      
  6. Compile and run the example using your favorite editor, or the command line:
  7. javac com/google/api/gbase/examples/basic/QueryExample3.java
    java com/google/api/gbase/examples/basic/QueryExample3
    

The output will be as follows:

<?xml version='1.0' encoding='UTF-8'?> 
  <feed>
    <id>http://base.google.com/base/feeds/items</id>
    <updated>2006-08-22T12:00:00.000Z</updated>
    <title type="text">Items matching query: [customer id(int):1870031]</title>
    ...

Stepping through the QueryExample3 code

As opposed to the previous examples, here we need to obtain an authorization token by authenticating with the Google Accounts server. We then use this authorization token to invoke displayMyItems():

public static void main(String[] args) throws IOException {
  QueryExample3 queryExample = new QueryExample3();
  String token = queryExample.authenticate();
  new QueryExample3().displayMyItems(token);
}

We authenticate using authentication for installed applications by making a POST request to AUTHENTICATION_URL.

private static final String AUTHENTICATION_URL = "https://www.google.com/accounts/ClientLogin";

The POST request is constructed in makeLoginRequest(). It looks like this:

// POST /accounts/ClientLogin HTTP/1.0
// Content-type: application/x-www-form-urlencoded
// Email=johndoe@gmail.com&Passwd=north23AZ&service=gbase&source=Insert Example

The service parameter is the name of the Google service for which authorization is being requested. For Google Base applications, it must be set to gbase, as in the above example. For more information, refer to the authorization documentation.

makeLoginRequest() sends the request to the Google Accounts server and returns the response as a String. A successful response will have the following structure:

//      HTTP/1.0 200 OK
//      Server: GFE/1.3
//      Content-Type: text/plain 
//      SID=DQAAAGgA...7Zg8CTN
//      LSID=DQAAAGsA...lk8BBbG
//      Auth=DQAAAGgA...dk3fA5N

authenticate() first obtains the authentication response from makeLoginRequest() and stores it in postOutput:

String postOutput = null;
try {
  URL url = new URL(AUTHENTICATION_URL);
  postOutput = makeLoginRequest(url);
} catch (IOException e) {
  System.out.println("Authentication error: " + e.toString());
}

It then tokenizes postOutput, and returns the next token after "Auth", or null if the token is not found:

StringTokenizer tokenizer = new StringTokenizer(postOutput, "=\n ");
String token = null;
    
while (tokenizer.hasMoreElements()) {
  if (tokenizer.nextToken().equals("Auth")) {
    if (tokenizer.hasMoreElements()) {
      token = tokenizer.nextToken(); 
    }
  break;
  }
}

The items are displayed in displayMyItems(), which is very similar to displayItems() in QueryExample1, except that it injects the authorization token (as well as the developer key) in the HTTP header. Developer keys can be specified both as a URL parameter (as in QueryExample1) and in the HTTP header (as in this example).

connection.setRequestProperty("Authorization", "GoogleLogin auth=" + token);
connection.setRequestProperty("X-Google-Key", "key=" + DEVELOPER_KEY);

Go to InsertExample