googleapis / google-api-php-client

A PHP client library for accessing Google APIs
http://googleapis.github.io/google-api-php-client/
Apache License 2.0
9.32k stars 3.52k forks source link

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

Closed najathi closed 1 year 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.

najathi commented 1 year ago

Is anyone here to answer for me?

saranshdhingra commented 1 year ago

Hi @najathi Sorry for the delay in response.

I tried your first approach and only added one small thing, i.e. the scope, https://www.googleapis.com/auth/contacts to the google client.

With that I was able to see the synced contact in my gmail.

Please try it with the scope added and let me know if that works or not.

Thanks.

najathi commented 1 year ago

Hi @najathi Sorry for the delay in response.

I tried your first approach and only added one small thing, i.e. the scope, https://www.googleapis.com/auth/contacts to the google client.

With that I was able to see the synced contact in my gmail.

Please try it with the scope added and let me know if that works or not.

Thanks.

@saranshdhingra

I tried earlier below the code, can you check it out, It's not working, it shows all creating contacts but contacts are not saved on google contacts.

$client = new \Google_Client();
$client->setClientId(config('google.client_id'));
$client->setClientSecret(config('google.client_secret'));
$client->setRedirectUri(config('google.redirect_uri'));
$client->setApplicationName(config('google.application_name'));
$client->setAccessType(config('google.access_type'));
$client->setScopes(config('google.scopes'));
$client->setApprovalPrompt(config('google.approval_prompt'));
$client->setPrompt(config('google.prompt'));
$client->setDeveloperKey(config('google.developer_key'));
$client->setAuthConfig(config('google.service.file'));

$service = new \Google_Service_PeopleService($client);
$newPerson = new \Google_Service_PeopleService_Person();

// Set the name of the new contact
$name = new \Google_Service_PeopleService_Name();
$name->setGivenName('John');
$name->setFamilyName('Doe');
$newPerson->setNames([$name]);

// Set the email address of the new contact
$email = new \Google_Service_PeopleService_EmailAddress();
$email->setValue('john.doe@example.com');
$newPerson->setEmailAddresses([$email]);

$createdPerson = $service->people->createContact($newPerson);
dd($createdPerson);

scopes are coming from google.php of config 'scopes' => [\Google\Service\Sheets::DRIVE, \Google\Service\Sheets::SPREADSHEETS, \Google\Service\PeopleService::CONTACTS, \Google\Service\PeopleService::CONTACTS_READONLY, \Google\Service\PeopleService::CONTACTS_OTHER_READONLY, \Google\Service\PeopleService::USERINFO_PROFILE],

If want to check the google config file. https://gist.github.com/najathi/f9c6ef76edb3aa86c0cc3cabda9e028c

saranshdhingra commented 1 year ago

So, I tried the exact same code with the only exception of the initial authorization code and I am able to see the new contacts in Google Contacts.

$client = new \Google_Client();
$client->setAuthConfig(''); // web credentials file
$client->setRedirectUri('');
$client->setScopes([\Google\Service\Sheets::DRIVE, \Google\Service\Sheets::SPREADSHEETS, \Google\Service\PeopleService::CONTACTS, \Google\Service\PeopleService::CONTACTS_READONLY, \Google\Service\PeopleService::CONTACTS_OTHER_READONLY, \Google\Service\PeopleService::USERINFO_PROFILE]);

Just to get it off our list, could you try the same with a different authorization technique?

saranshdhingra commented 1 year ago

Hi @najathi Were you able to test the approach with a different auth technique?

saranshdhingra commented 1 year ago

Closing this for now. But, please feel to reopen this if this issue pops up again.