Anonymous Explorer
Anonymous Explorer

Reputation: 31

google people api: assign/create contact group

I cannot figure out how to create a new contact group and assign it to the contact using google people api in php. An error

"person.memberships is a read only field."

occurs at $person->setMemberships():

    $contactGroup=new Google_Service_PeopleService_ContactGroup();
    //$contactGroup->setGroupType('USER_CONTACT_GROUP');
    $contactGroup->setName('Some group');
    $contactGroup->create();
    $cgm=new Google_Service_PeopleService_ContactGroupMembership();
    $cgm->setContactGroupId('groupID');
    $membership=new Google_Service_PeopleService_Membership();

    $membership->setContactGroupMembership($cgm);
      $person=new Google_Service_PeopleService_Person();

      $groupMemberships=array(($membership));
      $person->setMemberships(array($groupMemberships));//error happens here

Anyone could help with a proper example of creating contact group and assigning it to the contact?

Upvotes: 2

Views: 1519

Answers (2)

Steve
Steve

Reputation: 1963

The following code assumes you have instantiated a Google_Client object, and have already created a person and know their ID.

Resource ID example,

$person_id = 'people/1234567890abcde';

Create a contact group,

$peopleService = new Google_Service_PeopleService($client);

$newContactGroup = new Google_Service_PeopleService_ContactGroup;
$newContactGroup->setName('New contact group');

$createContactGroupRequest = new Google_Service_PeopleService_CreateContactGroupRequest;
$createContactGroupRequest->setContactGroup($newContactGroup);

$contactGroup = $peopleService->contactGroups->create($createContactGroupRequest);

$contact_group_id = $contactGroup->getResourceName();

Add a person to contact group,

$peopleService = new Google_Service_PeopleService($googleClient);

$modifyContactGroupMembersRequest = new Google_Service_PeopleService_ModifyContactGroupMembersRequest;
$modifyContactGroupMembersRequest->setResourceNamesToAdd($person_id);

$peopleService->contactGroups_members->modify($contact_group_id, $modifyContactGroupMembersRequest);

Upvotes: 4

Amos Yuen
Amos Yuen

Reputation: 1459

You can't set members of the contact group in the creation call. You need to create the contact group in one call, then in a second call add it using members.modify api call.

Upvotes: 0

Related Questions