python - python中的googleapiclient,使用updateContact返回403
问题描述
我正在尝试从谷歌的 API 获取联系人列表,然后更新其中的一些。在运行该功能时,我每次都会updateContact
收到 403 错误。
我正在使用以下代码读取联系人的 etag:Request person.etag is different than the current person.etag. Clear local cache and get the latest person."
service = build('people', 'v1', credentials=creds)
results = service.people().connections().list(
resourceName='people/me',
pageSize=100,
personFields='names,emailAddresses').execute()
connections = results.get('connections', [])
然后对于contact
中的每一个,我都在connections
尝试更改联系人 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,同时仅更改了两件事:
权限
添加您的代码
SCOPES = ['https://www.googleapis.com/auth/contacts'] ... service = build('people', 'v1', credentials=creds) results = service.people().connections().list( resourceName='people/me', pageSize=5, personFields='names,emailAddresses').execute() connections = results.get('connections', []) for contact in connections: service.people().updateContact(resourceName=contact['resourceName'], updatePersonFields="names", body={ "etag": contact['etag'], "names": [ { "familyName": "new family name", "givenName": "new given name" } ] } ).execute()
推荐阅读
- php - 在url php中解码ascii
- ios - 为什么 Geofire 在 iOS 上不起作用?- 离子
- wxpython - wx.html2.WebView 和屏幕阅读器
- google-api - 用于显示 pdf 的嵌入式 google drive api 返回 204
- rust - 如何防止最后一个参数需要用 clap 引用?
- google-app-engine - 如何将私有、自托管的 NPM 包与 Google App Engine 节点、标准环境一起使用
- python - 从网站提取的值制作 .xls 文件
- python - 如何获取 django 变量的子字符串 [HTML]
- xpath - 让 XPath 停在某个深度?
- oauth-2.0 - 在 oauth2 中使用“使用 slack 登录”后如何自动注销用户