无法在 PHP 中使用 Google 联系人 API V3 创建 contact/group

Unable to create a contact/group using Google Contacts API V3 in PHP

我知道在 Whosebug 上有一些关于此的问题,但 none 的答案似乎解决了我的问题。我正在使用 Google PHP Api 客户端库 v2.0.1,该应用程序是服务器到服务器应用程序。我的应用程序可以从 API 中读取,例如我可以列出 contacts/groups,但我无法:

  1. 准确创建联系人
  2. 创建联系人组

以下代码用于创建联系人:

putenv('GOOGLE_APPLICATION_CREDENTIALS='.APP_PATH.'access_token.json');

define('CREDENTIALS_PATH',APP_PATH .'access_token.json');
define('CLIENT_SECRET_PATH', APP_PATH. 'client_secret.json');

define('SCOPES', implode(' ', [
       'https://www.google.com/m8/feeds'
    ]
));

$client = new Google_Client();
$client->setApplicationName('My Contacts App');
$client->setDeveloperKey($devKey);
$client->setSubject("user@domain.com");
$client->setAccessType('offline');
$client->setIncludeGrantedScopes(true);
$client->useApplicationDefaultCredentials();
$client->addScope(SCOPES);

// Get Guzzle Client
$httpClient = $client->authorize();

$xml = '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:gd="http://schemas.google.com/g/2005">
<atom:category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
 <gd:givenName>Elizabeth</gd:givenName>
 <gd:familyName>Bennet</gd:familyName>
 <gd:fullName>Elizabeth Bennet</gd:fullName>
</gd:name>
<atom:content type="text">Notes</atom:content>
<gd:email rel="http://schemas.google.com/g/2005#work"
primary="true"
address="liz@gmail.com" displayName="E. Bennet"/>
<gd:email rel="http://schemas.google.com/g/2005#home"
address="liz@example.org"/>
</atom:entry>';

$headers = [
    'Content-Type' => 'application/atom+xml'
];

$response = $httpClient->post(
    'https://www.google.com/m8/feeds/contacts/default/full',
    $headers,
    $xml
);

echo $response->getBody();

返回以下响应:

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xlns:batch="http://schemas.google.com/gdata/batch" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:gd="http://schemas.google.com/g/2005">
<id>http://www.google.com/m8/feeds/contacts/user@domain.com/base/fe770968a626294</id>
<updated>2017-01-13T08:45:06.790Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<title type="text"/>
<link rel="http://schemas.google.com/contacts/2008/rel#edit-photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/user@domain.com/fe770968a626294/1B2M2Y8AsgTpgAmY7PhCfg"/>
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/user@domain.com/full/fe770968a626294"/>
<link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/user@domain.com/full/fe770968a626294/1484297106790001"/>
</entry>

这会导致创建一个没有属性集的空联系人。任何人都可以看到我要去哪里错了。非常感谢任何帮助。

解决方案

我错误地执行了 Guzzle 请求 - 应该有 RTM

对于任何感兴趣的人,代码的最后一部分应该是这样的:

$request = new \GuzzleHttp\Psr7\Request(
    'POST',
    'https://www.google.com/m8/feeds/contacts/default/full',
    ['Content-Type' => 'application/atom+xml; charset=UTF-8; type=entry'],
    $xml
);

$response = $httpClient->send($request);

如果有人想要在 server-to-server 应用程序中创建联系人的完整解决方案,就是这样:

putenv('GOOGLE_APPLICATION_CREDENTIALS='.APP_PATH.'access_token.json');

define('CREDENTIALS_PATH',APP_PATH .'access_token.json');
define('CLIENT_SECRET_PATH', APP_PATH. 'client_secret.json');

define('SCOPES', implode(' ', [
       'https://www.google.com/m8/feeds'
   ]
));

$client = new Google_Client();
$client->setApplicationName('My Contacts App');
$client->setDeveloperKey($devKey);
$client->setSubject("user@domain.com");
$client->setAccessType('offline');
$client->setIncludeGrantedScopes(true);
$client->useApplicationDefaultCredentials();
$client->addScope(SCOPES);

// Get Guzzle Client
$httpClient = $client->authorize();

$xml = '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:givenName>Elizabeth</gd:givenName>
<gd:familyName>Bennet</gd:familyName>
<gd:fullName>Elizabeth Bennet</gd:fullName>
</gd:name>
<atom:content type="text">Notes</atom:content>
<gd:email rel="http://schemas.google.com/g/2005#work" primary="true" address="liz@gmail.com" displayName="E. Bennet"/>
<gd:email rel="http://schemas.google.com/g/2005#home" address="liz@example.org"/>
</atom:entry>';

$request = new \GuzzleHttp\Psr7\Request(
   'POST',
   'https://www.google.com/m8/feeds/contacts/default/full',
   ['Content-Type' => 'application/atom+xml; charset=UTF-8; type=entry'],
   $xml
);

$response = $httpClient->send($request);