title: Download a spreadsheet from Google Drive / Workspace using Python
body: <p>Can you produce a Python example of how to download a Google Sheets spreadsheet given its key and worksheet ID (<code>gid</code>)? I can't.</p>
  <p>I've scoured versions 1, 2 and 3 of the API. I'm having no luck, I can't figure out their  compilcated ATOM-like feeds API, the <code>gdata.docs.service.DocsService._DownloadFile</code> private method says that I'm unauthorized, and I don't want to write an entire Google Login authentication system myself. I'm about to stab myself in the face due to frustration.</p>
  <p>I have a few spreadsheets and I want to access them like so:</p>
  <pre><code>username = 'mygooglelogin@gmail.com'
  password = getpass.getpass()
  
  def get_spreadsheet(key, gid=0):
      ... (help!) ...
  
  for row in get_spreadsheet('5a3c7f7dcee4b4f'):
      cell1, cell2, cell3 = row
      ...
  </code></pre>
  <p>Please save my face.</p>
  <hr />
  <p><strong>Update 1:</strong> I've tried the following, but no combination of <code>Download()</code> or <code>Export()</code> seems to work. (Docs for <code>DocsService</code> <a href="http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.docs.service.html" rel="nofollow noreferrer">here</a>)</p>
  <pre><code>import gdata.docs.service
  import getpass
  import os
  import tempfile
  import csv
  
  def get_csv(file_path):
    return csv.reader(file(file_path).readlines())
  
  def get_spreadsheet(key, gid=0):
    gd_client = gdata.docs.service.DocsService()
    gd_client.email = 'xxxxxxxxx@gmail.com'
    gd_client.password = getpass.getpass()
    gd_client.ssl = False
    gd_client.source = &quot;My Fancy Spreadsheet Downloader&quot;
    gd_client.ProgrammaticLogin()
  
    file_path = tempfile.mktemp(suffix='.csv')
    uri = 'http://docs.google.com/feeds/documents/private/full/%s' % key
    try:
      entry = gd_client.GetDocumentListEntry(uri)
  
      # XXXX - The following dies with RequestError &quot;Unauthorized&quot;
      gd_client.Download(entry, file_path)
  
      return get_csv(file_path)
    finally:
      try:
        os.remove(file_path)
      except OSError:
        pass
  </code></pre>