使用Google域共享联系人API未返回任何联系人

时间:2014-07-22 19:22:49

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

我正在谷歌应用领域进行教育,并且我尝试使用域共享联系人API来访问共享联系人。我正在使用带有服务帐户的google php api客户端,我已启用了正确的API并添加了类似于此答案的步骤1-5的相应范围:Is it possible to use "Domain-wide Delegation of Authority" with gdata-python-client?

启用了共享联系人;我可以使用Gmail。此外,我委托超级管理员用户。当我运行我的脚本时,我得到200响应,但响应正文中没有联系人或xml。

这是我的剧本:

<?php
require_once 'google-api-php-client/src/Google_Client.php';

const CLIENT_ID = 'xxxxxx.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxx@developer.gserviceaccount.com';
const KEY_FILE = '/path/to/yyyyy-privatekey.p12';

$client = new Google_Client();
$client->setApplicationName('Directory');
$client->setUseObjects(true);
$client->setClientId(CLIENT_ID);

session_start();

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}

// Load the key in PKCS 12 format
$key = file_get_contents(KEY_FILE);

$auth = new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.google.com/m8/feeds'), $key);
$auth->sub = 'superadmin@domain.com'; 

$client->setAssertionCredentials($auth);

$client->getAuth()->refreshTokenWithAssertion(); //normally would check if expired, but for dev refresh every time


$req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/domain.com/full"); //shared contacts
$req->setRequestHeaders(array('GData-Version'=> '3.0','content-type'=>'application/atom+xml; charset=UTF-8; type=feed'));


$responseObj = $client->getIo()->authenticatedRequest($req);
var_dump($responseObj);

$responseXml =$responseObj->getResponseBody();

if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
}

以下是回复:

object(Google_HttpRequest)#6 (10) {
    ["batchHeaders":"Google_HttpRequest":private]=>
        array(4) {
            ["Content-Type"]=> string(16) "application/http"
            ["Content-Transfer-Encoding"]=> string(6) "binary"
            ["MIME-Version"]=> string(3) "1.0"
            ["Content-Length"]=> string(0) ""
        }
    ["url":protected]=> string(55) "https://www.google.com/m8/feeds/contacts/domain.com/full"
    ["requestMethod":protected]=> string(3) "GET"
    ["requestHeaders":protected]=>
        array(3) {
            ["gdata-version"]=> string(3) "3.0"
            ["content-type"]=> string(46) "application/atom+xml; charset=UTF-8; type=feed"
            ["authorization"]=> string(138) "Bearer #a#a.#a#a#a#a-#-..."
        }
    ["postBody":protected]=>NULL
    ["userAgent":protected]=> string(37) "Directory google-api-php-client/0.6.5"
    ["responseHttpCode":protected]=> int(200)
    ["responseHeaders":protected]=>
        array(14) {
            ["content-type"]=> string(46) "application/atom+xml; charset=UTF-8; type=feed"
            ["expires"]=> string(29) "Tue, 22 Jul 2014 14:51:21 GMT"
            ["date"]=> string(29) "Tue, 22 Jul 2014 14:51:21 GMT"
            ["cache-control"]=> string(49) "private, max-age=0, must-revalidate, no-transform"
            ["vary"]=> string(44) "Accept, X-GData-Authorization, GData-Version"
            ["gdata-version"]=> string(3) "3.1"
            ["etag"]=> string(28) "W/"#a#a#a#a#a#a""
            ["last-modified"]=> string(29) "Tue, 22 Jul 2014 14:51:21 GMT"
            ["transfer-encoding"]=> string(7) "chunked"
            ["x-content-type-options"]=> string(7) "nosniff"
            ["x-frame-options"]=> string(10) "SAMEORIGIN"
            ["x-xss-protection"]=> string(13) "1; mode=block"
            ["server"]=> string(3) "GSE"
            ["alternate-protocol"]=> string(8) "443:quic"
        }
    ["responseBody":protected]=> string(1477) 
        "<?xml version='1.0' encoding='UTF-8'?>
            <feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/&quot;#a#a#a#a#a#a.&quot;'>
                <id>domain.com</id><updated>2014-07-22T14:51:21.805Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
                <title>domain.com's Contacts</title>
                <link rel='alternate' type='text/html' href='http://www.google.com/'/>
                <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/domain.com/full'/>
                <link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/domain.com/full'/>
                <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/domain.com/full/batch'/>
                <link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/domain.com/full?max-results=25'/>
                <author><name>(unknown)</name><email>domain.com</email></author>
                <generator version='1.0' uri='http://www.google.com/m8/feeds'>Contacts</generator>
                <openSearch:totalResults>0</openSearch:totalResults>    
                <openSearch:startIndex>1</openSearch:startIndex>
                <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
            </feed>"
    ["accessKey"]=> NULL
}

正如您所看到的,totalResults列表为0.我相信我正在进行身份验证,因为我有200响应,如果我将$auth->sub和Google_HttpRequest中的网址更改为特定用户,我可以访问其联系人。我不确定我错过了什么。

1 个答案:

答案 0 :(得分:0)

  

我委托超级管理员用户

...但您是否添加了域名共享联系人?请注意,这与从您的帐户委派(共享)到其他特定用户(例如您上面提到的超级管理员用户)的联系人不同。

我的理解是使用/m8/feeds/contacts/domain.com/full网址上的insert命令添加域共享联系人,而不是通过GMail UI添加。