2017-01-13 2 views
1

Je sais qu'il y a quelques questions à ce sujet sur stackoverflow mais aucune des réponses ne semble résoudre mon problème. J'utilise la bibliothèque client PHP Api de Google v2.0.1 et l'application est une application de serveur à serveur. Mon application peut lire à partir de l'API par exemple, je peux la liste des contacts/groupes, mais je ne suis pas capable:Impossible de créer un contact/groupe en utilisant Google Contacts API V3 en PHP

  1. Créer un contact avec précision
  2. Créer un groupe de contact

Le code ci-dessous pour créer un 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(); 

La réponse suivante est renvoyée:

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

Un contact vide est créé sans attributs définis. Quelqu'un peut-il voir où je vais mal? Toute aide est très appréciée.

Solution

Je remplissais la demande Guzzle mal - devrait avoir RTM

Pour tous ceux qui veulent la partie Guzzle finale du code devrait ressembler à ceci:

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

vous devriez publier votre solution comme réponse. – KENdi

+0

merci @KENdi J'ai posté la solution comme réponse – amburnside

Répondre

1

Dans le cas où quelqu'un veut la solution complète pour créer un contact dans une application de serveur à serveur, c'est celui-ci:

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