pulkitjalan / google-apiclient

Google api php client wrapper with Cloud Platform and Laravel support
MIT License
246 stars 75 forks source link

Google PeopleAPI: Created contact not saved or synced in Google Contacts #64

Closed najathi closed 6 days ago

najathi commented 1 year ago

I am using the Google People API to create a new contact in Google Contacts but it is not saved or synced in Google Contacts. I'm not getting any errors.

Here is the code I am using to create the contact:

$client = new GoogleClient(config('google'));
$googleClient = $client->getClient();
$peopleService = new PeopleService($googleClient);

$newContact = new \Google\Service\PeopleService\Person();
$newContact->setNames([new Google_Service_PeopleService_Name(['givenName' => 'John', 'familyName' => 'Doe'])]);
$newContact->setEmailAddresses([new Google_Service_PeopleService_EmailAddress(['value' => 'johndoe@example.com'])]);

$respose = $peopleService->people->createContact($newContact);
dd($respose); // it rerurns respose of person

The $newContact variable contains the newly created contact's resource name. I have also tried requesting a sync for the connection, as follows:

    $client = new GoogleClient(config('google'));
    $googleClient = $client->getClient();
    $peopleService = new PeopleService($googleClient);

    $params = [
        'personFields' => 'metadata,names,emailAddresses,phoneNumbers',
        'pageSize' => 2000,
        'requestSyncToken' => true
    ];

    $fullSyncResponse = $peopleService->people_connections->listPeopleConnections('people/me', $params);
    while ($fullSyncResponse->getNextPageToken() != null) {
        $fullSyncResponse = $peopleService->people_connections->listPeopleConnections('people/me', [
            'personFields' => 'metadata,names,emailAddresses,phoneNumbers',
            'pageSize' => 2000,
            'requestSyncToken' => true,
            'pageToken' => $fullSyncResponse->getNextPageToken()
        ]);
    }

    // Fetch incremental changes using the sync token returned in the last fullSyncResponse.
    // try {
        $incrementalSyncResponse = $peopleService->people_connections->listPeopleConnections('people/me', [
            'personFields' => 'metadata,names,emailAddresses,phoneNumbers',
            'pageSize' => 2000,
            'syncToken' => $fullSyncResponse->getNextSyncToken()
        ]);

        if ($incrementalSyncResponse->getConnections() != null)
            foreach ($incrementalSyncResponse->getConnections() as $person) {
                $this->handlePerson($person);
            }

        while ($incrementalSyncResponse->getNextPageToken() != null) {
            $incrementalSyncResponse = $peopleService->people_connections->listPeopleConnections('people/me', [
                'personFields' => 'metadata,names,emailAddresses,phoneNumbers',
                'pageToken' => $incrementalSyncResponse->getNextPageToken(),
                'pageSize' => 2000,
                'syncToken' => $fullSyncResponse->getNextSyncToken()
            ]);

            if ($incrementalSyncResponse->getConnections() != null)
                foreach ($incrementalSyncResponse->getConnections() as $person) {
                    $this->handlePerson($person);
                }
        }

        dd($fullSyncResponse, $incrementalSyncResponse); // it rerurns respose of listPeopleConnections
    // 
} catch (\Exception $ex) {}

public function handlePerson(PeopleService\Person $person)
    {
        if ($person->getMetadata()->getDeleted()) {
            // Handle deleted person
        } else {
            // Handle changed person
        }
    }

However, the contact is still not saved or synced in Google Contacts.

Can anyone please advise on what might be causing this issue, and how I can get the newly created contact to be saved and synced in Google Contacts? Thank you in advance for your help.

pulkitjalan commented 6 days ago

Sorry, I've never used the api and its a little out of scope of this package. Thanks.