python - 如何与人 API 创建新联系人

标签 python post google-cloud-platform google-api google-people-api

我正在尝试使用 python 和人员 API 在公司中创建多个联系人。

我进行了研究,发现我需要使用人员 API 来通过 API 编辑联系人,但我没有找到如何完成此操作的良好示例。

我正在使用以下命令来制作我的联系人的简单列表:

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
    import urllib3

    # 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 10 connection names')
        results = service.people().connections().list(
            resourceName='people/me',
            pageSize=10,
            personFields='names,phoneNumbers').execute()
        connections = results.get('connections', [])

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

    if __name__ == '__main__':
        main()

输出:

List 10 connection names
Eeverton None
Evetton None
Paulinha None
Wayne +5521992*****
Joao Pedro +55219643*****
Mae +552199*****
Maae +552199*****
Advogado Gb +5521964*****

工作完美。

但我需要的是创建新的联系人。

  1. 我更改了范围

  2. 我验证了 oauth2

  3. 我已正确存储所有文件和 key (token.picke 和credentials.json)

我应该如何创建新联系人?函数还是 POST?

您能否提供一个用于创建联系人的简单代码示例?

https://developers.google.com/people/api/rest/v1/people/createContact

最佳答案

适用于:

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

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
    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)

    # CREATING CONTACTSSSSSSSS
    service.people().createContact( body={
        "names": [
            {
                "givenName": "Samkit"
            }
        ],
        "phoneNumbers": [
            {
                'value': "8600086024"
            }
        ],
        "emailAddresses": [
            {
                'value': '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2d3f3335372a6b6a676b1e39333f3732703d3133" rel="noreferrer noopener nofollow">[email protected]</a>'
            }
        ]
    }).execute()



if __name__ == '__main__':
    main()

关于python - 如何与人 API 创建新联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59145135/

相关文章:

python - Python 中的类

html - Django/Div ID

google-cloud-platform - 有没有办法通过docker容器内的代理访问谷歌云SQL

firebase - 我怎么知道谁更改了我的 Cloud Function

python - 如何从图像中读取标点符号,如 '/' 、 '_' 和 '\'

python - 如何终止蝗虫负载测试中的特定线程

PHP:当名称没有数组时检索复选框的值

java - 如何在谷歌云中部署Spring Boot应用程序?

python - Pandas 中基于行选择的多列替换

php - MySQL查询无法读取PHP变量