简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM