将电子表格上传到Google文档会导致502请求错误

时间:2014-03-03 14:46:33

标签: python gdata

我只是尝试通过gdata包将电子表格上传到Google文档。代码运行成功! csv正确地出现在谷歌文档上,但我收到此错误,终止了我的代码......

  

追踪(最近一次通话):    文件“test3.py”,第21行,in      entry = gd_client.Upload(ms,'Backup.gpg')#,folder_or_uri = uri    在上传文件“C:\ Python27 \ lib \ site-packages \ gdata \ docs \ service.py”,第306行      folder_or_uri)    _UploadFile中的文件“C:\ Python27 \ lib \ site-packages \ gdata \ docs \ service.py”,第161行      转换器= gdata.docs.DocumentListEntryFromString)    邮件中的文件“C:\ Python27 \ lib \ site-packages \ gdata \ service.py”,第1236行      media_source = media_source,converter = converter)    PostOrPut中的文件“C:\ Python27 \ lib \ site-packages \ gdata \ service.py”,第1358行      'reason':server_response.reason,'body':result_body}   gdata.service.RequestError:{'status':502,'body':'\ n \ n \ n \ n错误502(服务器错误)!! 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;填充: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}一个img {border:0} @media屏幕和(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){#login {background:url (//www.google.com/images/errors/logo_sm_2_hr.png)无重复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){#logon {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。该\ xe2 \ x80 \ x99s出现错误。\ n

服务器遇到临时错误,无法完成您的请求。

请重试30秒。那个\ xe2 \ x80 \ x99s我们都知道。\ n','reason':'Bad Gateway'}

我的代码如下

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')

1 个答案:

答案 0 :(得分:0)

经过多次搜索后,我发现了这个链接:How to upload documents with new python-gdata (2.0.16)?并且很容易将其调整到我的问题中。问题是我的代码已经过时了以前的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)

我还发现此资源有助于理解参数:https://gdata-python-client.googlecode.com/hg/pydocs/