Reputation: 121
I'm trying to create a new contact through google people api + PHP (Server-to-server) using the code below and the code apparently runs without errors, but no contacts are created at: https://www.google.com/contacts/
Querying the contacts as described here (https://developers.google.com/people/quickstart/php#step_3_set_up_the_sample) I see that the contact was created.
I use Google App Suite.
Is there anything wrong with my code?
<?php
require $_SERVER['DOCUMENT_ROOT'].'/../vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('my configs...');
$client->addScope(Google_Service_PeopleService::CONTACTS);
$service = new Google_Service_PeopleService($client);
$person = new Google_Service_PeopleService_Person();
$email = new Google_Service_PeopleService_EmailAddress();
$email->setValue('[email protected]');
$person->setEmailAddresses($email);
$name = new Google_Service_PeopleService_Name();
$name->setDisplayName('User de Test');
$person->setNames($name);
$exe = $service->people->createContact($person);
print_r($exe);
Upvotes: 0
Views: 2977
Reputation: 382
i think you should try this php library it did the same task for me https://github.com/rapidwebltd/php-google-contacts-v3-api
Installation :-
-download the repo git clone https://github.com/rapidwebltd/php-google-contacts-v3-api
-then run composer require rapidwebltd/php-google-contacts-v3-api
-create api credential on google api here https://console.developers.google.com/
-then rename the .config_blank.json
to .config.json
and update the clientID
,clientSecret
to the credential you get from the api console
-update the redirectUri
in the .config.json file to the full path to your redirect-handler.php
file
-then go ahead and open authorise-application.php
in your browser & open the provided link in a new tab to authorize your app then this page will redirect you to the file redirect-handler.php
and will ask you to add the hash providedd to the .config.json
file for the attribute refreshToken
-done you can try this code to create new contact in your account
require_once __DIR__ . '/vendor/autoload.php';
$contact->name = 'Test';
$contact->phoneNumber = '07812363789';
$contact->email = '[email protected]';
$contact->content = 'Note for example';
$contactAfterUpdate =rapidweb\googlecontacts\factories\ContactFactory::submitUpdates($contact);
var_dump($contactAfterUpdate);
Update:
as DivineOmega's comment
there is a new version of the API Here you can try it.
Upvotes: 1