在People API中删除联系人功能不起作用:方法:people.deleteContact

时间:2020-07-13 16:18:35

标签: python python-3.x google-api google-contacts-api google-people-api

现在,我尝试使用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()

但是创建联系人def bellow也可以。

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)

1 个答案:

答案 0 :(得分:1)

资源名称格式不正确:

如果您尝试通过Try this API from this page删除联系人,则会发现要访问的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()

参考: