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:
Refer to the Customer Items feed reference for more information about the items feed.
You will need a Google account email and password properly set up for Google Base 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.
QueryExample3.java and fill in the DEVELOPER_KEY static string with your developer key. private static final String DEVELOPER_KEY = "";
EMAIL and PASSWORD static strings:private static final String EMAIL = ""; private static final String PASSWORD = "";
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>
...
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);
The authorization token is used for authenticating the user. It does not ensure that the user has correctly set up hers/his account for Google Base. If this is the case, all queries send to the items feed will return an error. The error message is in XML format and can be parsed to obtain the error description:
final StringBuilder errorReason = new StringBuilder();
DefaultHandler errorHandler = new DefaultHandler() {
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("error")) {
errorReason.append(attributes.getValue("reason"));
}
}
};
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
ParserAdapter adapter = new ParserAdapter(parser.getParser());
adapter.setContentHandler(errorHandler);
adapter.parse(responeXML);
Go to InsertExample