InsertExample shows how to add your own item to Google Base.
The previous examples demonstrated how to query the Google Base data API
server, both for snippets (unauthenticated feeds) and for items
(authenticated feeds, containing a specific customer's items.) InsertExample demonstrates how to add content to Google Base so
that the world can see it.
You will need a Google Account email and password in order to run this example.
In this example we will connect to your "items" feed and perform an HTTP POST
operation to add a new item. The item
to be added is encoded in Atom format, and is defined a String constant.
private static final String DATA_ITEM = "<?xml version=\'1.0\'?>\n" + "<entry xmlns=\'http://www.w3.org/2005/Atom\' xmlns:g=\'http://base.google.com/ns/1.0\'>\n" + " <category scheme=\'http://base.google.com/categories/itemtypes\' term=\'Products\'/>\n" + " <g:item_type type=\'text\'>Products</g:item_type>\n" + " <title type=\'text\'>My cool car is for sale</title>" + " <content type=\'xhtml\'>Light pink, yellow seats.</content>" + "</entry>";
The item we add is a very simple item, consisting only of an item type ("products", in
our case), a title, and content (a description). Feel free to change these
fields to contain your personalized items, or add new attributes and labels. You can use the responses dumped by QueryExample1 and QueryExample3 as an inspiration for adding new attributes.
InsertExample.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/InsertExample.java java com/google/api/gbase/examples/basic/InsertExample
The output will be as follows:
Obtained authorization token: DQAAAGgA...dk3fA5N <?xml version='1.0' encoding='UTF-8'?> <entry> <id>http://base.google.com/base/feeds/items/16024998325761524417</id> <published>2006-08-23T15:18:55.184Z</published> <updated>2006-08-23T15:18:55.184Z</updated> <category scheme="http://base.google.com/categories/itemtypes" term="Products"/> <title type="text">My cool car is for sale</title> <content type="xhtml">Light pink, yellow seats.</content> <link rel="self" type="application/atom+xml" href="http://base.google.com/base/feeds/items/16024998325761524417"/> <link rel="edit" type="application/atom+xml" href="http://base.google.com/base/feeds/items/16024998325761524417"/> <g:item_type type="text">Products</g:item_type> </entry>
We use the same feed as in QueryExample3.java to insert the item:
private static final String ITEMS_FEED = "http://base.google.com/base/feeds/items";
Authentication is also performed as in QueryExample3, using makeLoginRequest() to
request an authorization token and authenticate() to parse the
authentication response. The insertion of the new data item is done in postItem(), which connects to the items feed:
HttpURLConnection connection = (HttpURLConnection)(new URL(ITEMS_FEED)).openConnection();
Once the connection is created, we need to set its properties: the HTTP request method, the content type of the information that is being posted, the authorization header, and the developer key:
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/atom+xml");
connection.setRequestProperty("Authorization", "GoogleLogin auth=" + token);
connection.setRequestProperty("X-Google-Key", "key=" + DEVELOPER_KEY);
We then obtain the output stream of the connection and dump DATA_ITEM into it:
OutputStream outputStream = connection.getOutputStream(); outputStream.write(DATA_ITEM.getBytes()); outputStream.close();
The rest of the method is already familiar: we obtain the response code and print out to the console the contents of the input stream that correspond to the response code.
Go to UpdateExample