繁体   English   中英

使用 Google People API + PHP 创建联系人(服务器到服务器)

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

我正在尝试使用下面的代码通过 google people api + PHP(服务器到服务器)创建一个新联系人,代码显然运行没有错误,但没有在以下位置创建联系人: https : //www.google.com /联系人/

按照此处所述查询联系人 ( https://developers.google.com/people/quickstart/php#step_3_set_up_the_sample ) 我看到联系人已创建。

我使用 Google 应用套件。

我的代码有什么问题吗?

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

我想你应该试试这个 php 库,它为我做了同样的任务https://github.com/rapidwebltd/php-google-contacts-v3-api

安装:-

- 下载 repo git clone https://github.com/rapidwebltd/php-google-contacts-v3-api

- 然后运行composer require rapidwebltd/php-google-contacts-v3-api

-在此处在 google api 上创建 api 凭据https://console.developers.google.com/

- 然后将.config_blank.json重命名为.config.json并将clientIDclientSecret更新为您从 api 控制台获得的凭据

-Update的redirectUri在.config.json文件的完整路径redirect-handler.php文件

- 然后继续在浏览器中打开authorise-application.php并在新选项卡中打开提供的链接以授权您的应用程序然后此页面会将您重定向到文件redirect-handler.php并要求您添加提供的哈希到属性refreshToken.config.json文件

- 完成后,您可以尝试使用此代码在您的帐户中创建新联系人

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

更新:
正如DivineOmega的评论,有一个新版本的 API在这里你可以试试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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