The Google Fusion Tables API v1 has been deprecated as of May 3rd, 2016. The API will continue to work until August 1st, 2017, and will shut down on that date. A migration guide is available outlining the steps to take to migrate to v2.

Google Fusion Tables API Sample Code

Docs List API Example

Fusion Tables is integrated with the Folder feed and the access control list feed in the Google Documents List API. This sample demonstrates use of the Documents List API to:

  • List tables
  • Set permissions on tables
  • Move a table to a folder

This sample depends on the gdata python client library described in the Google Documents List API V3.0

#!/usr/bin/python
#
# Copyright (C) 2011 Google Inc.

"""
Demonstrates use of the Documents List API to:
- List tables
- Set permissions on tables
- Move a table to a folder

This sample depends on the gdata python client library described here:
http://code.google.com/apis/documents/docs/3.0/developers_guide_python.html
"""

import gdata.docs.data
import gdata.docs.client
import gdata.acl.data


CONSUMER_KEY = "<consumer_key>"
CONSUMER_SECRET = "<consumer_secret>"
CALLBACK = "<callback_uri>"
SCOPES = ["https://docs.google.com/feeds/"]

class DocsListHelper():
  def __init__(self, appname):
    self.client = gdata.docs.client.DocsClient(source=appname)
    self.client.ssl = True  # Force all API requests through HTTPS
    self.client.http_client.debug = False  # Set to True for debugging HTTP requests

  def authorize(self):
    request_token = self.client.GetOAuthToken(
      SCOPES, CALLBACK, CONSUMER_KEY, consumer_secret=CONSUMER_SECRET)
    return request_token, request_token.generate_authorization_url()

  def getToken(self, request_token, uri):
    request_token = gdata.gauth.AuthorizeRequestToken(request_token, uri)
    access_token = self.client.GetAccessToken(request_token)
    self.client.auth_token = gdata.gauth.OAuthHmacToken(CONSUMER_KEY,
      CONSUMER_SECRET,
      access_token.token,
      access_token.token_secret,
      gdata.gauth.ACCESS_TOKEN)

  # Print the list of tables in the docs list
  def printTableList(self):
    feed = self.client.GetDocList(uri="/feeds/default/private/full/-/table")
    self._printTableFeed(feed)

  # Returns a single doc entry for a given table name
  # If multiple tables have the same name, only the first will be returned
  def getDocEntryForTable(self, tablename):
    feed = self.client.GetDocList(
        uri="/feeds/default/private/full/-/table?title=%s&title-exact=true&max-results=1" % (tablename))
    if not feed.entry:
      return None
    for entry in feed.entry:
      return entry

  # Prints the permissions for a doc entry
  def printPermissions(self, doc_entry):
    acl_feed = self.client.GetAclPermissions(doc_entry.resource_id.text)
    self._printAclFeed(acl_feed)

  # Sets the permissions for a given table name
  # Permissions is a list of tuples [("username", "type", "role"), ...]
  # username is a gmail address, a google group address, a google apps domain
  # type is user, group, or domain (the type corresponds to the username)
  # role can be owner, writer, or reader
  def setPermissions(self, tablename, permissions):
    doc_entry = self.getDocEntryForTable(tablename)
    if not doc_entry:
      print "Sorry, this table does not exist"
      return

    for username,type,user_role in permissions:
      scope = gdata.acl.data.AclScope(value=username, type=type)
      role = gdata.acl.data.AclRole(value=user_role)
      acl_entry = gdata.docs.data.Acl(scope=scope, role=role)
    
      new_acl = self.client.Post(acl_entry, doc_entry.GetAclFeedLink().href)
      print "%s %s added as a %s" % \
          (new_acl.scope.type, new_acl.scope.value, new_acl.role.value)

  # Move a table to a folder given the table name and folder name
  def moveTableToFolder(self, tablename, foldername):
    doc_entry = self.getDocEntryForTable(tablename)
    folder_entry = self._getFolderEntry(foldername)
    if not doc_entry:
      print "Sorry, this table does not exist"
      return
    if not folder_entry:
      print "Sorry, this folder does not exist"
      return

    new_entry = self.client.Move(doc_entry, folder_entry)
    print "%s moved into %s" % (doc_entry.title.text, folder_entry.title.text)
    return new_entry

  # Returns the folder entry for a given folder name
  def _getFolderEntry(self, foldername):
    feed = self.client.GetDocList(
        uri="/feeds/default/private/full/-/folder?title=%s&title-exact=true&max-results=1" % (foldername))
    if not feed.entry:
      return None
    for entry in feed.entry:
      return entry

  # Prints some info about each table in the feed
  def _printTableFeed(self, feed):
    print "\n"
    if not feed.entry:
      print "No entries in feed.\n"
    for entry in feed.entry:
      print entry.title.text.encode("UTF-8"), entry.GetDocumentType(), entry.resource_id.text

      # List folders the document is in.
      for folder in entry.InFolders():
        print folder.title

  # Prints some info about the acls of a table
  def _printAclFeed(self, feed):
    for acl in feed.entry:
      print "%s (%s) is %s" % (acl.scope.value, acl.scope.type, acl.role.value)

if __name__ == "__main__":
  docsListHelper = DocsListHelper("fusiontables-v1")

  # Authorize with OAuth 1.0
  request_token, auth_url = docsListHelper.authorize()
  print auth_url
  url = raw_input("Visit the url above in a browser, authorize, and paste the resulting url here: ")
  docsListHelper.getToken(request_token, url)

  # Print the list of tables
  docsListHelper.printTableList()

  # Print the permissions for a doc entry
  doc_entry = docsListHelper.getDocEntryForTable("New Table")
  docsListHelper.printPermissions(doc_entry)

  # Update the permissions for a given table
  new_permissions = [("an.email@gmail.com","user","reader")]
  docsListHelper.setPermissions("New Table", new_permissions)

  # Move a table to a new folder
  docsListHelper.moveTableToFolder("New Table", "Fusion Tables")