简体   繁体   中英

Create Contact with Google People API + PHP (Server to Server)

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('test@test.com');
$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);

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 = 'test@example.com';
$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.

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