无法使用PHP中的Google Contacts API V3创建联系人/群组

时间:2017-01-13 08:52:51

标签: php google-api-php-client google-contacts guzzle6

我知道在stackoverflow上有一些关于这个的问题,但没有一个答案似乎可以解决我的问题。我使用的是Google PHP Api客户端库v2.0.1,该应用程序是服务器到服务器的应用程序。我的应用程序可以从API中读取,例如我可以列出联系人/组,但我无法:

  1. 准确创建联系人
  2. 创建联系人组
  3. 以下代码用于创建联系人:

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

1 个答案:

答案 0 :(得分:1)

如果任何人想要在服务器到服务器应用程序中创建联系人的完整解决方案,就是这样:

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