首页 > 解决方案 > People API中的删除联系人功能不起作用:方法:people.deleteContact

问题描述

现在我尝试使用 python 为联系人编写一个 CMS 项目,但我遇到了一些问题。

我对此 def 进行了编码,但它不适用于 google people API。 https://developers.google.com/people/v1/contacts

日志

File "/Users/nguyenngoclinh/.conda/envs/1z_vietnam/lib/python3.7/site-packages/googleapiclient/http.py", line 907, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://people.googleapis.com/v1/%7B'people/c9194159806299427121'%7D:deleteContact?alt=json returned "Not Found">

删除功能如下

def delete_contacts_with_resourceName(creds,http,number_of_contact):
    # Call the People API
    service = build('people', 'v1', credentials=creds)
    print('Ban dang xoa', number_of_contactcontact, 'contacts')

    results = service.people().connections().list(
        resourceName='people/me',
        pageSize=number_of_contactofcontact,
        personFields='names,emailAddresses,phoneNumbers,emailAddresses,addresses').execute()
    service = discovery.build('people', 'v1', http=http,
                              discoveryServiceUrl='https://people.googleapis.com/$discovery/rest')
    connections = results.get('connections', [])
    for person in connections:
        abcd = person.get('resourceName')
        service.people().deleteContact(resourceName={abcd}).execute()

但是下面的 creat contact def 也可以。

def creat_a_google_contact(http):
    # POST / v1 / people: createContact
    # HTTP / 1.1
    # Body: {"names": [{"givenName": "John", "familyName": "Doe"}]}
    # Host: people.googleapis.com
    service = discovery.build('people', 'v1', http=http,
                              discoveryServiceUrl='https://people.googleapis.com/$discovery/rest')
    service.people().createContact(body={
        "names": [
            {
                'givenName': "Nguyen Ngoc Linh",
                "familyName": "29N2359 BMW 325i"
            }
        ],
        "phoneNumbers": [
            {
                'value': "0979955664"
            }
        ],
        "emailAddresses": [
            {
                'value': "bk.nguyenlinh@gmail.com"
            }
        ],
        "addresses": [
            {
                "streetAddress": "So 1 ngo 85 Lang Ha",
                "extendedAddress": "Ba Dinh",
                "city": "Ha Noi",
                "region": "Ha Noi",
                "postalCode": "10000",
                "country": "Vietnam",
                "countryCode": "84"
            }
        ]
    }).execute()

def main,请任何人帮助我

def main():
    creds = get_credentials(FLOW)
    # print_list_google_contact_with_number_of_contacts(creds,1000)
    http=get_http(creds)
    #creat_a_google_contact(http)
    # print_result(creds)
    delete_contacts_with_resourceName(creds,http,1000)
    #print_resourceName(creds, 2000)

标签: pythonpython-3.xgoogle-apigoogle-contacts-apigoogle-people-api

解决方案


资源名称格式不正确:

如果您尝试通过Try this API 从此页面删除联系人,您会注意到要访问的 URL 具有以下形状:

https://people.googleapis.com/v1/people/c7142462727258425368:deleteContact

people/c7142462727258425368联系人在哪里resourceName。也就是说,资源名称没有单引号(' '),也没有括号({ })。由于资源名称可能没有在 URL 中格式化,API 无法识别它,从而导致 404 错误

这就是您的请求失败的原因:

https://people.googleapis.com/v1/%7B'people/c9194159806299427121'%7D:deleteContact

要解决此问题,只需在将abcd其作为资源名称提供时删除括号。它应该是这样的:

service.people().deleteContact(resourceName=abcd).execute()

参考:


推荐阅读