使用新的People API创建Google联系人,并将该联系人注册到多个组/标签中,可以按照以下步骤进行操作:
以下是一个示例Python代码,演示如何使用People API创建Google联系人并将其注册到多个组/标签中:
import googleapiclient.discovery
from google.oauth2 import service_account
# 设置API凭据
credentials = service_account.Credentials.from_service_account_file(
'path/to/service_account_key.json',
scopes=['https://www.googleapis.com/auth/contacts']
)
# 创建People API客户端
service = googleapiclient.discovery.build('people', 'v1', credentials=credentials)
# 创建联系人对象
contact = {
'names': [
{
'givenName': 'John',
'familyName': 'Doe'
}
],
'phoneNumbers': [
{
'value': '+1234567890'
}
],
'emailAddresses': [
{
'value': 'johndoe@example.com'
}
]
}
# 创建联系人
created_contact = service.people().create(body=contact).execute()
# 获取用户的所有组/标签
groups = service.contactGroups().list().execute()
# 选择要添加联系人的目标组/标签
target_groups = [group for group in groups['contactGroups'] if group['name'] in ['Family', 'Friends']]
# 将联系人添加到每个目标组/标签中
for group in target_groups:
service.contactGroups().members().modify(
resourceName=group['resourceName'],
body={'resourceNamesToAdd': [created_contact['resourceName']]}
).execute()
这是一个基本的示例,你可以根据自己的需求进行修改和扩展。请确保在实际使用中遵循Google API的最佳实践和安全规范。
领取专属 10元无门槛券
手把手带您无忧上云