amburnside
amburnside

Reputation: 1903

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

I know there are a few questions about this on stackoverflow but none of the answers appear to solve my problem. I am using the Google PHP Api client library v2.0.1 and the application is a server-to-server application. My application can read from the API eg I can list contacts/groups, but I am unable to:

  1. Create a contact accurately
  2. Create a contact group

The code below is for creating a contact:

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("[email protected]");
$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="[email protected]" displayName="E. Bennet"/>
<gd:email rel="http://schemas.google.com/g/2005#home"
address="[email protected]"/>
</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();

The following response is returned:

<?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/[email protected]/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/[email protected]/fe770968a626294/1B2M2Y8AsgTpgAmY7PhCfg"/>
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/[email protected]/full/fe770968a626294"/>
<link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/[email protected]/full/fe770968a626294/1484297106790001"/>
</entry>

This results in an empty contact being created with no attributes set. Can anyone see where I am going wrong. Any help is much appreciated.

Solution

I was performing the Guzzle Request incorrectly - should have RTM

For anyone who is interested the final guzzle part of the code should have looked like this:

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

Upvotes: 2

Views: 471

Answers (1)

amburnside
amburnside

Reputation: 1903

In case any one wants the full solution for creating a contact in a server-to-server application, this is it:

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("[email protected]");
$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="[email protected]" displayName="E. Bennet"/>
<gd:email rel="http://schemas.google.com/g/2005#home" address="[email protected]"/>
</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);

Upvotes: 3

Related Questions