2013-11-03 71 views
3

當我使用「此解決方案」時,我得到一個空白的聯繫人數組。Google Contact Api PHP - 服務器帳戶(服務器到服務器):空聯繫人列表

我在example/contacts文件夾中使用帶有serviceAccount.php的「google-api-php-client-0.6.7」,並在src/contrib文件夾中使用了Google_ContactsService。

我已將Google Apis Access配置爲OAuth 2作爲服務器帳戶。我有p12文件,客戶端ID和密碼郵件地址。項目名稱是「Google Contacts Sample」。

serviceAccount.php:

<?php 
require_once '../../src/Google_Client.php'; 
require_once '../../src/contrib/Google_ContactsService.php'; 

const CLIENT_ID = 'xxxxxxxxxxxxx.apps.googleusercontent.com'; 
const SERVICE_ACCOUNT_NAME = '[email protected]'; 
const KEY_FILE = '/absolute_path/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-privatekey.p12'; 

$client = new Google_Client(); 
$client->setApplicationName("Google Contacts Sample"); 

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

$key = file_get_contents(KEY_FILE); 
$client->setAssertionCredentials(new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.googleapis.com/auth/contacts'), $key)); 
$client->setClientId(CLIENT_ID); 

if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion(); 
} 
$token = $client->getAccessToken(); 

$service = new Google_ContactsService($client); 
$result = $service->all(); 
print '<h2>Contacts Result:</h2><pre>' . print_r($result, true) . '</pre>'; 

Google_ContactsService.php:

<?php 
class Google_ContactsService 
{ 

    const SCOPE = "https://www.google.com/m8/feeds"; 

    /** 
    * @var Google_Client 
    */ 
    private $client; 

    public function __construct($pClient) 
    { 
     $this->client = $pClient; 
     $this->client->setScopes(self::SCOPE); 
    } 

    public function all() 
    { 
     $result = $this->execute('default/full?max-results=999'); 
     $contacts = array(); 

     foreach($result["feed"]["entry"] as $entry) 
     { 
      if(!isset($entry['gd$email'])) 
       $entry['gd$email'] = array(); 
      if(!isset($entry['gd$phoneNumber'])||empty($entry['gd$phoneNumber'])) 
       continue; 

      $phones = array(); 
      $emails = array(); 

      foreach($entry['gd$phoneNumber'] as $phone) 
      { 
       $phone['$t'] = preg_replace('/\+33/', "0", $phone['$t']); 
       $phone['$t'] = preg_replace('/\-/', '', $phone['$t']); 
       $phones[] = $phone['$t']; 
      } 

      foreach($entry['gd$email'] as $email) 
      { 
       $emails[] = $email['address']; 
      } 

      $contacts[] = array(
       "fullName"=>utf8_decode($entry['title']['$t']), 
       "phones"=>$phones, 
       "emails"=>$emails 
      ); 
     } 

     return $contacts; 
    } 

    private function execute($pUrl) 
    { 
     $oauth = Google_Client::$auth; 
     $request = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/".$pUrl."&alt=json"); 
     $oauth->sign($request); 
     $io = Google_Client::$io; 

     $result_json = $io->makeRequest($request)->getResponseBody(); 
     $result = json_decode($result_json, true); 
     return $result; 
    } 
} 

當我去 「http://server.com/packs/googleapi/examples/contacts/serviceAccount.php」 我看不出有任何接觸。

該函數執行返回空。

我該怎麼辦?

謝謝。

+0

感謝您的代碼。幫助了我的情況:) – DevZer0

回答

1

我意識到這是舊的,所以你可能已經移動,但爲了別人的發現,我相信你沒有看到任何聯繫人的原因是,你沒有委託給特定的用戶。不計爲Google Apps專業版和教育版域名上的共享聯繫人,聯繫人對該用戶是私有的。

我會改變這條線

$client->setAssertionCredentials(new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.googleapis.com/auth/contacts'), $key)); 

更喜歡這個

$auth = new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.google.com/m8/feeds'), $key); 
$auth->sub = '[email protected]'; //whoever's contacts you want to see 
$client->setAssertionCredentials($auth); 

如果你看看Google_AssertionCredentials方法簽名你可以在頭兩行保持在一起,但我認爲這使它更明確。取決於你想達到的目標,[email protected]可能是一個從某種輸入,數據庫等設置的變量。

相關問題