【问题标题】:Create Contact with Google People API + PHP (Server to Server)使用 Google People API + PHP(服务器到服务器)创建联系人
【发布时间】:2017-10-31 00:28:18
【问题描述】:

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

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

我使用 Google App Suite。

我的代码有什么问题吗?

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

【问题讨论】:

  • Is there anything wrong with my code? 你告诉我们。脚本有效吗?有什么错误吗?它有什么问题?
  • 您首先需要代表您的应用与 google 创建一个 OAUTH 会话,以便在您的帐户上创建联系人。如果您使用的是服务帐户 ( two-legged-oauth ),然后在不先创建会话的情况下添加联系人,它可能被保存在服务帐户范围内

标签: php google-api


【解决方案1】:

我认为你应该试试这个 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

-在这里https://console.developers.google.com/在google api上创建api凭据@

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

-将 .config.json 文件中的 redirectUri 更新为 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的评论 有新版本的APIHere 你可以试试。

【讨论】:

  • 在链接到外部资源时,请考虑解释如何使用该库,以及如何解决上述答案中的问题。
  • 感谢您提及我们的 Google 通讯录库。我们实际上创建了一个使用 Google People API 的新库。可能值得更新您的答案以包含它。 github.com/rapidwebltd/php-google-people-api
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
相关资源
最近更新 更多