python - 批量删除联系人报 "If-Match or If-None-Match header or entry etag attribute required"错误

标签 python google-api google-contacts-api

我正在使用 gdata Python 库来批量删除联系人,但我只是收到“需要 If-Match 或 If-None-Match header 或条目 etag 属性”错误。

我认为当我必须在控制台中启用 Contacts API 时问题就开始了(直到几天前还不需要?*)。

编辑:

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

尝试指定 If-Match header ,但仍然失败:

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
)

还创建了一个 ticket在项目页面上,但我怀疑它会在那里引起任何关注。

编辑 2:

使用带有 force=TrueBatch 方法(它只是添加了 If-Match: * header )是相同的结果。

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

* 有人可以验证吗?我以前从来不需要在控制台中启用它,我的应用程序能够毫无问题地使用 Contacts API,而且我相信它以前甚至不可用。昨天看的我很吃惊。

最佳答案

正在从 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')

关于python - 批量删除联系人报 "If-Match or If-None-Match header or entry etag attribute required"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23576729/

相关文章:

YouTube API - 列出我管理的所有 channel

c# - 验证 Google 凭据

python - 更改youtube/dailymotion/vimeo嵌入大小

python - argparse:位置参数的默认值不起作用?

python - Windows 10 上的多处理 python 3.6 无法正常工作

python - Python 中的 Google Places API

C# 应用程序 - 读取 Google 通讯录

google-contacts-api - 保存来自 Chrome DevTools 的 HTTP 请求

python - 如何在 Python 中创建用户定义的列表?