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

标签 python resources google-docs-api

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

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

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

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 属性。

您可以使用 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

如果您愿意,您可以向库维护者报告错误(如果尚未报告)。

关于python - 使用 Python 从 Google 文档中删除资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9940578/

相关文章:

java - OAuth - 无效 token : Request token used when not allowed

进行 groupby 计数时出现 python pandas 错误

python - 如何在 Python 3 中将字典列表保存到 .mat 文件中?

python - 将数据从虚拟打印机传递到 python

c - 从控制台应用程序显示系统资源使用情况

html - 使用 Google Doc AddOn 的 Apps 脚本处理 'drop' 事件

google-apps-script - 如何使用谷歌应用程序脚本阅读谷歌文档中的标题引用

python - 从 python 中的另一个排序列表中删除排序列表的快速和 pythonic/clean 方法是什么?

c# - 在哪里放置测试数据?

java - 从jar中读取资源文件