Download Google Spreadsheet and save as xls
<p>Am trying to write python procedure to download a spreadsheet from google spreedsheets and save it as .xls.
here is my code</p>

<pre><code>import os
import sys
from getpass import getpass

import gdata.docs.service
import gdata.spreadsheet.service



'''
    get user information from the command line argument and 
    pass it to the download method
'''
def get_gdoc_information():
    email ="mygmailaccount"
    password ="mypassword"
    gdoc_id = ['google_id1','googleid2','googleidn']
    for doc_id in gdoc_id:
        try:
            download(doc_id, email, password)
        except Exception, e:
            raise e

#python gdoc.py 1m5F5TXAQ1ayVbDmUCyzXbpMQSYrP429K1FZigfD3bvk#gid=0
def download(doc_id, email, password, download_path=None, ):
    print "Downloading the XLS file with id %s" % doc_id

    gd_client = gdata.docs.service.DocsService()


    #auth using ClientLogin
    gs_client = gdata.spreadsheet.service.SpreadsheetsService()
    gs_client.ClientLogin(email, password)

    #getting the key(resource id and tab id from the ID)

    resource    = doc_id.split('#')[0]
    tab         = doc_id.split('#')[1].split('=')[1]
    resource_id = 'spreadsheet:'+resource

    if download_path is None:
        download_path = os.path.abspath(os.path.dirname(__file__))

    file_name = os.path.join(download_path, '%s.xls' % (doc_id))

    print 'Downloading spreadsheet to %s...' % file_name

    docs_token = gd_client.GetClientLoginToken()
    gd_client.SetClientLoginToken(gs_client.GetClientLoginToken())
    gd_client.Export(resource_id, file_name, gid=tab)
    gd_client.SetClientLoginToken(docs_token)

    print "Download Completed!"


if __name__=='__main__':
    get_gdoc_information()
</code></pre>

<p>Whenever I try to run it, i get a gdata error below</p>

<pre><code>gdata.service.RequestError: {'status': 401, 'body': '&lt;HTML&gt;\n&lt;HEAD&gt;\n&lt;TITLE&gt;Unauthorized&lt;/TITLE&gt;\n&lt;/HEAD&gt;\n&lt;BODY BGCOLOR="#FFFFFF" TEXT="#000000"&gt;\n&lt;H1&gt;Unauthorized&lt;/H1&gt;\n&lt;H2&gt;Error 401&lt;/H2&gt;\n&lt;/BODY&gt;\n&lt;/HTML&gt;\n', 'reason': 'Unauthorized'}
</code></pre>

<p>Am using gdata library.
I have been struggling with this whole day and cant seem to figure out what's happening.
Can anyone please figure out and assit?
Any other minimal script that can achieve my purpose as described above is will be much appreciated.
Thank you</p>