gpt4 book ai didi

python - 通过 Google People API 创建新联系人

转载 作者:行者123 更新时间:2023-12-03 08:50:48 24 4
gpt4 key购买 nike

是否可以使用 Google People API 创建 Google 联系人?

我在使用 Google API 创建新联系人时遇到问题。

我搜索了几天并找到了以下信息:

1 - 看来 People API 包将取代 Google Contacts API

https://gsuite-developers.googleblog.com/2017/07/google-people-api-now-supports-updates.html

2 - 许多人无法使用 gdata 和atom 包通过 python 3+ 创建新联系人。

3 - People API 按照 Gsuite 的推荐显示

https://support.google.com/a/answer/6103110?hl=pt-BR

我想知道是否有人使用这些 Google People API 创建新联系人。

是否需要 G Suite 电子邮件?

如何获取访问 token ?

我已经在 Google 云平台上完成了所有设置(启用 API 和 auth2),我有 json 文件、 key 和客户端 ID

编辑:

我设法使用此代码列出我的 50 个联系人,我必须修改 block 以创建新联系人

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/contacts']

def main():
"""Shows basic usage of the People API.
Prints the name of the first 10 connections.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)

service = build('people', 'v1', credentials=creds)

# Call the People API
print('List 50 connection names')
results = service.people().connections().list(
resourceName='people/me',
pageSize=50,
personFields='names,emailAddresses').execute()
connections = results.get('connections', [])

for person in connections:
names = person.get('names', [])
if names:
name = names[0].get('displayName')
print(name)

if __name__ == '__main__':
main()

最佳答案

由于您已经具有列出联系人的权限,因此您应该能够执行类似以下操作来创建联系人:

newContact = { "names": [{ "givenName": "John", "familyName": "Doe" }] }
result = service.people().createContact(body=newContact).execute()

body /人的完整定义是 here .

关于python - 通过 Google People API 创建新联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59127105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com