TAGS :Viewed: 7 - Published at: a few seconds ago

[ Uploading a spreadsheet to google docs results in 502 request error ]

I'm simply attempting to upload a spreadsheet to google docs via the gdata package. And it the code runs successfully! The csv appears on google docs correctly but I receive this error which terminates my code...

Traceback (most recent call last): File "test3.py", line 21, in entry = gd_client.Upload(ms, 'Backup.gpg')#, folder_or_uri=uri File "C:\Python27\lib\site-packages\gdata\docs\service.py", line 306, in Upload folder_or_uri) File "C:\Python27\lib\site-packages\gdata\docs\service.py", line 161, in _UploadFile converter=gdata.docs.DocumentListEntryFromString) File "C:\Python27\lib\site-packages\gdata\service.py", line 1236, in Post media_source=media_source, converter=converter) File "C:\Python27\lib\site-packages\gdata\service.py", line 1358, in PostOrPut 'reason': server_response.reason, 'body': result_body} gdata.service.RequestError: {'status': 502, 'body': '\n\n \n \n Error 502 (Server Error)!!1\n \n {margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px} > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}\n \n \n

502.That\xe2\x80\x99s an error.\n

The server encountered a temporary error and could not complete your request.

Please try again in 30 seconds. That\xe2\x80\x99s all we know.\n', 'reason': 'Bad Gateway'}

My code is the following

import os
import sys
import gdata.docs
import gdata.docs.service
import gdata.docs.client

email = 'change_this@gmail.com'
password = 'this_too'

gd_client = gdata.docs.service.DocsService()
gd_client.ClientLogin(email, password)

f = open('test.csv')
ms = gdata.MediaSource(file_handle=f, content_type='text/csv', content_length=os.path.getsize(f.name))
entry = gd_client.Upload(ms, 'Backup')

Answer 1


After much searching I found this link: How to upload documents with new python-gdata (2.0.16)? and easily adapted it to my problem. The issue was that my code had become outdated since previous versions of gdata.

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

email = 'change_this@gmail.com'
password = 'this_too'

client = gdata.docs.client.DocsClient(source='poetic-harmony-508')
client.api_version = "3"
client.ssl = True
client.ClientLogin(email, password, client.source)

filePath = "path/filename.csv"
newResource = gdata.docs.data.Resource(filePath, "csv")

media = gdata.data.MediaSource()
media.SetFileHandle(filePath, 'text/csv')

newDocument = client.CreateResource(newResource, create_uri=gdata.docs.client.RESOURCE_UPLOAD_URI, media=media)

I also found this resource helpful in understanding the parameters: https://gdata-python-client.googlecode.com/hg/pydocs/