pythonflaskgoogle-people-api

Need Assistance in Updating Multiple fields of a contact using Google's People API in python


I have been trying to update multiple fields of a contact using the Google People API. Here is my code and the error I keep getting.

from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import os

# Define the scopes required for accessing the People API
SCOPES = ['https://www.googleapis.com/auth/contacts']

def get_credentials():
    creds = None
    # The file token.json 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.json'):
        creds = Credentials.from_authorized_user_file('token.json')
    # 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.json', 'w') as token:
            token.write(creds.to_json())
    return creds

def update_contacts(contact_id, given_name, family_name, email_address, phone_number):
    creds = get_credentials()
    new_data = {
        'names': [{'givenName': given_name, 'familyName': family_name}],
        'emailAddresses': [{'value': email_address}],
        'phoneNumbers': [{'value': phone_number}]
    }
    service = build('people', 'v1', credentials=creds)
    contact = service.people().get(resourceName=contact_id).execute()
    contact.update(new_data)
    updated_contact = service.people().updateContact(
        resourceName=contact_id,
        # updatePersonFields='names,emailAddresses,phoneNumbers',
        personFields = 'names,emailAddresses,phoneNumbers',
        body=contact
    ).execute()
    print('Contact updated:', updated_contact)

# Example usage:
# contact_id = 'people/c2288946713327522882'
    
# update_contacts(contact_id, given_name, family_name, email_address, phone_number)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://people.googleapis.com/v1/people/c2288946713327522882?alt=json returned "personFields mask is required. Please specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.". Details: "personFields mask is required. Please specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.">

I've tried adding the personFields instead of the updatePersonFields as the error suggest with no success. From my research on SO I was able to update one field. However, updating more than one field has proven hard. Kindly help.


Solution

  • Modification points:

    When these points are reflected in your script, how about the following modification?

    Modified script:

    In this modification, please modify your function update_contacts as follows.

    def update_contacts(contact_id, given_name, family_name, email_address, phone_number):
        creds = get_credentials()
        new_data = {
            'names': [{'givenName': given_name, 'familyName': family_name}],
            'emailAddresses': [{'value': email_address}],
            'phoneNumbers': [{'value': phone_number}]
        }
        service = build('people', 'v1', credentials=creds)
        contact = service.people().get(resourceName=contact_id, personFields="names,emailAddresses,phoneNumbers").execute()
        contact.update(new_data)
        updated_contact = service.people().updateContact(
            resourceName=contact_id,
            updatePersonFields='names,emailAddresses,phoneNumbers',
            personFields='names,emailAddresses,phoneNumbers',
            body=contact
        ).execute()
        print('Contact updated:', updated_contact)
    

    Note:

    References: