Python 获得;如果匹配或不匹配,则需要标题或条目etag属性";批量删除联系人时出错

Python 获得;如果匹配或不匹配,则需要标题或条目etag属性";批量删除联系人时出错,python,google-api,google-contacts-api,Python,Google Api,Google Contacts Api,我使用gdataPython库对联系人进行批量删除,只得到“If Match或If None Match头或entry etag属性required”错误 我想问题是从我必须在控制台中启用Contacts API时开始的(直到几天前还不需要它?*) 编辑: 实际上,更新和删除操作都失败了。批量插入工作良好 尝试指定If Match标题,但仍然失败: custom_headers = atom.client.CustomHeaders(**{'If-Match': '*'}) request_fe

我使用
gdata
Python库对联系人进行批量删除,只得到“If Match或If None Match头或entry etag属性required”错误

我想问题是从我必须在控制台中启用Contacts API时开始的(直到几天前还不需要它?*)

编辑

实际上,更新和删除操作都失败了。批量插入工作良好

尝试指定
If Match
标题,但仍然失败:

custom_headers = atom.client.CustomHeaders(**{'If-Match': '*'})
request_feed = gdata.contacts.data.ContactsFeed()
request_feed.AddDelete(entry=contact, batch_id_string='delete')
response_feed = self.gd_client.ExecuteBatch(
        request_feed,
        'https://www.google.com/m8/feeds/contacts/default/full/batch',
        custom_headers=custom_headers
)
在项目页面上也创建了一个,但我怀疑它是否会得到任何关注

编辑2:

使用
Batch
方法和
force=True
(如果匹配:标题,则只添加
)得到相同的结果

response_feed = self.gd_client.Batch(
    request_feed,
    uri='https://www.google.com/m8/feeds/contacts/default/full/batch',
    force=True
)

*有人能证实这一点吗?我以前从未在控制台中启用过它,我的应用程序能够毫无问题地使用Contacts API,我相信它以前是不可用的。昨天看到它我很惊讶

原始帖子中引用的票证有一些更新信息和临时解决方法,允许批量删除成功。到目前为止,它对我有效


从谷歌代码票据中复制答案

基本上,您需要修补客户端的
Post
方法来稍微修改请求提要。这里有一种不直接修改库源的方法:

def patched_post(client, entry, uri, auth_token=None, converter=None, desired_class=None, **kwargs):
    if converter is None and desired_class is None:
        desired_class = entry.__class__
    http_request = atom.http_core.HttpRequest()
    entry_string = entry.to_string(gdata.client.get_xml_version(client.api_version))
    entry_string = entry_string.replace('ns1', 'gd')  # where the magic happens
    http_request.add_body_part(
        entry_string,
        'application/atom+xml')
    return client.request(method='POST', uri=uri, auth_token=auth_token,
                          http_request=http_request, converter=converter,
                          desired_class=desired_class, **kwargs)

# when it comes time to do a batched delete/update,
# instead of calling client.ExecuteBatch, instead directly call patched_post
patched_post(client_instance, entry_feed, 'https://www.google.com/m8/feeds/contacts/default/full/batch')

您还可以指定etag属性来绕过它。这在批处理请求有效负载中起作用:

  <entry   gd:etag="*"  >
     <batch:id>delete</batch:id>
     <batch:operation type="delete"/>
     <id> urlAsId </id>
  </entry>

删除
乌拉西德

iam也面临同样的问题,在不了解gdata库的详细信息的情况下,是否应该以某种方式将自定义\u headers实例传递给请求?或者可以将其安装在gd_客户端上
?我怀疑它仅仅通过创建一个CustomHeaders实例就可以添加标题。啊,是的,很好。那是个打字错误。这对你有帮助吗?还是这个