2014-07-22 74 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