批量删除联系人时出现"If-Match或If-None-Match header或entry etag attribute required"错误

joh*_*n2x 11 python google-api google-contacts-api

我正在使用gdataPython库来完成批量删除的联系人,我只是得到"If-Match或If-None-Match标头或条目etag属性需要"错误.

我认为问题是在我必须在控制台中启用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
)
Run Code Online (Sandbox Code Playgroud)

还在项目页面上创建了一张票,但我怀疑它会在那里得到任何关注.

编辑2:

使用Batch带有force=True(只添加If-Match: *标题)的方法是相同的结果.

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

*有人可以验证吗?我以前从未在控制台中启用它,我的应用程序能够毫无问题地使用Contacts API,我相信它之前甚至都没有.昨天我很惊讶.

joh*_*n2x 4

从 Google 代码票中复制答案。

基本上,您需要修补客户端的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')
Run Code Online (Sandbox Code Playgroud)