我正试图从谷歌的API获取联系人列表,然后更新其中的一些联系人。在运行updateContact
函数时,我每次都会收到403个错误Request person.etag is different than the current person.etag. Clear local cache and get the latest person."
。
我正在使用以下代码读取联系人的etag:
service = build('people', 'v1', credentials=creds)
results = service.people().connections().list(
resourceName='people/me',
pageSize=100,
personFields='names,emailAddresses').execute()
connections = results.get('connections', [])
然后,对于connections
中的每个contact
,我尝试更改联系人的givenName和familyName:
service.people().updateContact(resourceName="contact's resource name",
updatePersonFields="names",
body={
"etag": "contact's etag",
"names": [
{
"familyName": "new family name",
"givenName": "new given name"
}
]
}
).execute()
联系人的etag
和resourceName
来自连接的列表响应。
例如:
print(connections[0])
{'resourceName': 'people/<resource_number>',
'etag': 'etag string',
'names': [{'metadata': {'primary': True,
'source': {'type': 'CONTACT', 'id': 'id number'}},
'displayName': 'x',
'familyName': 'x',
'givenName': 'x',
'displayNameLastFirst': 'x',
'unstructuredName': 'x'}]}
运行准确的代码确实有效
我运行了您的代码并基于Google People Python Quickstart,同时只更改了两件事:
权限
添加代码
相关问题 更多 >
编程相关推荐