使用Python从Google文档中删除资源

2024-09-30 01:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用以下功能删除Google Docs中的电子表格:

def f_DeleteResource(xls_name):
  """Delete a resource"""
  client=Auth()
  for e1 in client.GetResources().entry:
    e2 = client.GetResource(e1)
    if xls_name==e2.title.text:
      client.DeleteResource(e2.resource_id.text,True)

当我更改client.DeleteResource(p1,p2)的第一个参数时,会得到不同的错误:

client.DeleteResource(e2.resource_id.text,True)

^{pr2}$

client.DeleteResource(e2,True)

Traceback (most recent call last):
File "C:\xmp\D6GDocsDeleteUpload.py", line 164, in <module> main()
File "C:\xmp\D6GDocsDeleteUpload.py", line 157, in main f_DeleteResource(sys.argv[2])
File "C:\xmp\D6GDocsDeleteUpload.py", line 144, in f_DeleteResource client.DeleteResource(e2,True)
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 543, in delete_resource return super(DocsClient, self).delete(uri, **kwargs)
File "C:\Python27\lib\site-packages\gdata\client.py", line 748, in delete **kwargs)
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 66, in request return super(DocsClient, self).request(method=method, uri=uri, **kwargs)
File "C:\Python27\lib\site-packages\gdata\client.py", line 319, in request RequestError)
gdata.client.RequestError: Server responded with: 403, <errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>matchHeaderRequired</code><location type='header'>If-Match|If-None-Match</location><internalReason>If-Match or If-None-Match header or entry etag attribute required</internalReason></error></errors>

有人能帮我吗?在


Tags: inpyclienttrueiflibpackagesline
2条回答

这似乎是googleapi Python库中的一个bug。我检查了gdata-2.0.16并注意到DeleteResource()函数只使用资源(gdata/docs)的URL/客户端.py第540-543行),但稍后检查hasattr(entry_or_uri,'etag')(gdata)/客户端.py第737-741行),当然字符串值(uri)没有etag属性。在

您可以使用force关键字参数来解决它:

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

client = gdata.docs.client.DocsClient()
client.ClientLogin('xxxxxx@gmail.com', 'xxxxxx', 'XxX')

for doc in client.GetAllResources():
    if doc.title.text == 'qpqpqpqpqpqp':
        client.DeleteResource(doc, force=True)
        break

如果需要,可以向库维护人员报告错误(如果尚未报告)。在

此版本已修复此问题: http://code.google.com/p/gdata-python-client/source/detail?r=f98fff494fb89fca12deede00c3567dd589e5f97

如果您将客户端同步到存储库,您应该能够删除资源,而不必指定“force=True”。在

相关问题 更多 >

    热门问题