2014-07-22 75 views
0

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

已启用共享联系人;我可以看到他们使用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 = '[email protected]'; 
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 = '[email protected]'; 

$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中的网址更改为特定用户,我可以访问他们的联系人。我不确定我错过了什么。

回答

0

我委托给超级管理员用户

...但你有没有添加任何域共享联系人?请注意,这与从您的帐户委派(分享)给其他特定用户(例如上面提到的超级用户)的联系人不同。

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

+0

嗨布莱克。我同意域共享联系人不是通过GMail UI添加的,但您可以在GMail中看到它们。我没有通过api添加域共享联系人,但是他们在管理控制台(admin.google.com)中的_Google Apps>联系人设置>高级设置_下启用。我已启用_启用联系人共享_并选择_显示所有电子邮件地址_和_仅显示域配置文件_。我没有尝试更改联系人管理器中显示的内容。 – Marty

+0

布莱克,你是对的;我没有任何外部域共享联系人。域内部的共享联系人必须通过Directory API访问。域名外部的共享联系人必须通过域名共享联系人API进行访问。在我的情况下,我的所有联系人都是内部的。一旦我更改了作用域并请求URL为目录特定的,我就可以访问这些联系人。 在https://developers.google.com/admin-sdk/domain-shared-contacts/上,它以粗体指定了外部联系人,但我不明白它对于域而不是用户来说意味着外部联系人。 – Marty