pythonapigoogle-apigoogle-api-python-clientgoogle-contacts-api

add new contact to group using google people API with python


I am trying to create a code that saves new contacts and puts them in a certain group.

Creating a contact works great-

service = build('people', 'v1', credentials=creds)
service.people().createContact(body={"names": [{"givenName": name}], "phoneNumbers": [{'value': phone}],}).execute()

There is no problem in creating a group either-

service = build('people', 'v1', credentials=creds)
a = service.contactGroups().create(body={"contactGroup": {"name": "try1123", }}).execute()

But when I try to create a contact within the group I encounter an unknown error-

    service.people().createContact(body={"names": [{"givenName": name}], "phoneNumbers": [{'value': phone_number}],
                                     "memberships": [{"contactGroupMembership": {
                                         "contactGroupResourceName": "contactGroups/'try1123'", }, }]}).execute()

error:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://people.googleapis.com/v1/people:createContact?alt=json returned "Request contains an invalid argument.". Details: "Request contains an invalid argument.">

createContact python code

createContact Method


Solution

  • I think the problem here is where the {contactGroupId} you are specifying in contactGroupResourceName, you are putting the name of the contact group you created, the id should be an int value for what can I see in the examples, something like "contactGroupResourceName": "contactGroups/123123123"

    Most likely the call when you created the contactGroup should have returned you this id.