本文介绍了使用Python从Google文档删除DeleteResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下功能删除Google文档中的电子表格:

I am trying to delete a spreadsheet in Google Docs with this function:

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)的第一个参数时,我得到了不同的错误:

And I obtain different errors when I change the first parameter of client.DeleteResource(p1,p2):

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

client.DeleteResource(e2.resource_id.text,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.resource_id.text,True)
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 540, in delete_resource uri = entry.GetEditLink().href
AttributeError: 'str' object has no attribute 'GetEditLink'

client.DeleteResource(e2,True) :

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>

有人可以帮助我吗?

推荐答案

这似乎是Google API Python库中的错误.我检查了 gdata-2.0.16 ,发现 DeleteResource()函数仅使用资源的URL(gdata/docs/client.py第540-543行),但后来检查 hasattr(entry_or_uri,'etag')(gdata/client.py第737-741行),当然字符串值(uri)没有 etag 属性.

It seems to be a bug in Google API Python library. I checked gdata-2.0.16 and noticed that DeleteResource() function uses only URL of the resource (gdata/docs/client.py lines 540-543), but later checks for hasattr(entry_or_uri, 'etag') (gdata/client.py lines 737-741) and of course string value (uri) doesn't have etag attribute.

您可以使用 force 关键字参数来解决该问题:

You may work around it using force keyword argument:

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

client = gdata.docs.client.DocsClient()
client.ClientLogin('[email protected]', 'xxxxxx', 'XxX')

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

如果需要,可以向库维护者报告错误(如果尚未报告).

If you want you may report an error to library maintainers (if it isn't already reported).

这篇关于使用Python从Google文档删除DeleteResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 16:34