Updating a Google Site Listpage/Listitem

Here is a quick and dirty (by fully functional) sample of updating a listpage/listitem in Google Sites using the Data API.


import getpass
import gdata.sites.client
import gdata.sites.data

USER = 'test@example.com'
DOMAIN = 'YOUR_DOMAIN'  # or 'site' if you're not using a Google Apps domain.

# Setup our client.
client = gdata.sites.client.SitesClient(source='google-SitesListeItemUpdateTest', site=SITE, domain=DOMAIN)
client.ClientLogin(USER, getpass.getpass(), client.source)
client.ssl = True
client.http_client.debug = False

# Only fetch listpages.
feed = client.GetContentFeed(uri=client.MakeContentFeedUri() + '?kind=listpage')

# Work with first listpage we found.
lp = feed.GetListPages()[0]

print 'Listpage columns:'
for col in lp.data.column:
  print 'index: %s, name: %s' % (col.index, col.name)

# Query the listpage's listems and work with first row found.
li = client.GetContentFeed(uri=lp.feed_link.href).entry[0]

print 'Row contents:'
for field in li.field:
  print 'index: %s, name: %s, value: %s' % (field.index, field.name, field.text)

# Update the first fields/column's value.
li.field[0].text = 'Someone else'
entry = client.Update(li)

# Update the listpage's column heading.
#lp.data.column[0].name = 'New Heading'
#entry2 = client.Update(lp)

Download your DocList arbitrary file uploads in .NET

The .NET client's Download() method currently handles exporting documents, presentations, and spreadsheets. However, it cannot download PDFs or arbitrary file types. Those types cannot be exported to different formats. Instead, you need to override the Download() method in docsrequest.cs to hit the content src link:

public Stream Download(Document document) {
  string queryUri = "";
  Service s = this.Service;

  string downloadUrl = document.DocumentEntry.Content.Src.ToString();
  Uri target = new Uri(downloadUrl);

  return s.Query(target);

Making a Calendar public via the ACL feed

Google Calendar allows individual calendars to be marked as public, meaning that they can be accessed either via the Calendar UI, embeded calendars, or the Calendar Data API without requiring a password.

Normally, this option is enabled from within the Calendar UI. However, it can be enabled using the Calendar Data API as well. To do so, you'd send the following HTTP request:

POST /calendar/feeds/default/acl/full
Host: www.google.com


In this case, this will make the default calendar as public. If you want to make a secondary calendar public, replace default in the request's location with the desired calendar ID.

Update a document's content in .NET

class Demo {
  private DocumentsService service = null;

  public Demo() {
    service = new DocumentsService("google-DocUpdateTip-v1");
    service.setUserCredentials("username@gmail.com", "pa$$word");
    GDataGAuthRequestFactory reqFactory = (GDataGAuthRequestFactory)service.RequestFactory;
    reqFactory.ProtocolMajor = 3;

  static void Main(string[] args) {
    Demo demo = new Demo();

    // Fetch only documents
    DocumentsListQuery query = new DocumentsListQuery("http://docs.google.com/feeds/default/private/full/-/document");
    DocumentsFeed doclistFeed = demo.service.Query(query);

    // Update first document found
    DocumentEntry entry = (DocumentEntry)doclistFeed.Entries[0];
    Console.WriteLine("Updating " + entry.Title.Text + "...");

    DocumentEntry updatedEntry = demo.UpdateDocContents(entry, "C:/Documents and Settings/replacement.doc");
    Console.WriteLine(entry.Title.Text + " Updated!, view at " + entry.AlternateUri.ToString());

  public DocumentEntry UpdateDocContents(DocumentEntry entryToUpdate, String replacementFileName) {
    FileInfo fileInfo = new FileInfo(replacementFileName);
    FileStream stream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

    DocumentEntry entry = null;

    try {
      // Convert the extension to caps and strip the "." off the front
      String ext = fileInfo.Extension.ToUpper().Substring(1);

      String contentType = (String)DocumentsService.GDocumentsAllowedTypes[ext];
      if (contentType == null) {
        throw new ArgumentException("File extension '" + ext + "' is not recognized as valid.");
      // Set ETag because we're making an update
      GDataRequestFactory factory = (GDataRequestFactory)service.RequestFactory;
      factory.CustomHeaders.Add("If-Match: " + entryToUpdate.Etag);

      Uri mediaUri = new Uri(entryToUpdate.MediaUri.ToString());
      entry = service.Update(mediaUri, stream, contentType, entryToUpdate.Title.Text) as DocumentEntry;
    } finally {

    return entry;

Create a new Google Docs Spreadsheet from content using AJAX

This example shows how one could use jQuery to make an AJAX request that creates new Google Docs Spreadsheet from existing text/csv content. This particular examples uses ClientLogin
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"

<script type="text/javascript">

function constructContentBody(docTitle, docType, contentBody, contentType) {
  var atom = ["<?xml version='1.0' encoding='UTF-8'?>",
              '<entry xmlns="http://www.w3.org/2005/Atom">',
              '<category scheme="http://schemas.google.com/g/2005#kind"',
              ' term="http://schemas.google.com/docs/2007#', docType, '"/>',
              '<title>', docTitle, '</title>',
  var body = ['--END_OF_PART\r\n',
              'Content-Type: application/atom+xml;\r\n\r\n',
              atom, '\r\n',
              'Content-Type: ', contentType, '\r\n\r\n',
              contentBody, '\r\n',
  return body;

function createSpreadsheetFromContent(title, content, contentType) {

    type: 'POST',
    url: 'http://docs.google.com/feeds/documents/private/full',
    contentType: 'multipart/related; boundary=END_OF_PART',
    data: constructContentBody(title, 'spreadsheet', content, contentType),
    dataType: 'xml',
    beforeSend: function(xhr) {
      xhr.setRequestHeader('Authorization', 'GoogleLogin auth=' + token);
    success: function(resp) {
      $('#data').html('Spreadsheet created!');

<div id="data"></div>

<button onclick="createSpreadsheetFromContent('ANewTitle', '1,2,3,4', 'text/csv')">Create spreadsheet w/ content</button>

Download a Google Doc using the PHP library

At the time of writing this tip, the Zend_Gdata_Docs component of the PHP library does not contain the export/download functionality of the DocList API. Here is an example of using AuthSub and file_get_contents() to download a document as a .txt file:

function download($client, $url, $format=null) {
  $sessionToken = $client->getHttpClient()->getAuthSubToken();
  $opts = array(
    'http' => array(
      'method' => 'GET',
      'header' => "GData-Version: 3.0\r\n".
                  "Authorization: AuthSub token=\"$sessionToken\"\r\n"
  if ($url != null) {
    $url =  $url . "&exportFormat=$format";
  return file_get_contents($url, false, stream_context_create($opts));

// TODO 1: setup a Zend_Gdata_Docs client in $docs_client
// TODO 2: fetch a $feed or $entry
$contentLink = $feed->entries[0]->content->getSrc();
$fileContents = download($docs_client, $contentLink, 'txt');
echo 'Contents of document "' . $feed->entries[0]->title . '":<hr>';
echo "<pre>$fileContents</pre>";

OAuth in Google App Engine

This sample demonstrates a basic structure that you can use to perform 3-legged OAuth using the Google Data Python client library in Google App Engine. This particular example talks to the Documents List Data API.

App Engine (Python) + OAuth sample

Note: The sample is available in two versions, one that signs requests with RSA-SHA1 and another that signs with HMAC-SHA1.