Download Google Spreadsheet and save as xls
Question: Am trying to write python procedure to download a spreadsheet from google
spreedsheets and save it as .xls. here is my 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()
    

Whenever I try to run it, i get a gdata error below

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

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


Answer: your error does suggest an login problem. Maybe you need to change your
settings in your google account or try another way of a login.

Try looking here: [SyntaxError using gdata-python-client to access Google Book
Search Data API](http://stackoverflow.com/questions/2925985/syntaxerror-using-
gdata-python-client-to-access-google-book-search-data-api/3013945#3013945)

or here: [Download a spreadsheet from Google Docs using
Python](http://stackoverflow.com/questions/3287651/download-a-spreadsheet-
from-google-docs-using-python)

I'm sorry to post this as an answer but I can't post comments yet.

Regards

