Delete contact function in People API not work: Method: people.deleteContact

Viewed 608

Now i try to use python to code one CMS project for contacts, but i have some problems.

I code this def but it is not work with google people API. https://developers.google.com/people/v1/contacts

The logs

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

delete fuction is bellow

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

But the creat contact def bellow also work.

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

The def main, please anyone help me

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 Answers

Resource name is not formatted correctly:

If you try deleting the contact via Try this API from this page, you will notice that the URL to access has this shape:

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

Where people/c7142462727258425368 is the contact's resourceName. That is to say, the resource name does not have single quotes (' ') nor brakets ({ }) around it. Since the resource name is not probably formatted in the URL, the API is not recognizing it, causing the 404 error.

That's what's failing in your request:

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

To fix this, just remove the brackets around abcd when you provide it as resource name. It should be like this:

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

Reference:

Related