3

当我使用“此解决方案”时,我得到一个空白联系人数组。

我将“google-api-php-client-0.6.7”与 serviceAccount.php 一起使用到示例/联系人文件夹中,并在 src/contrib 文件夹中使用 Google_ContactsService。

我已经配置了 Google API 访问,使用 OAUTH 2 作为服务器帐户。我有 p12 文件、客户端 ID 和客户端邮件地址。项目名称为“Google Contacts Sample”。

服务帐户.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 = 'xxxxxxxxxxxxx@developer.gserviceaccount.com';
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 ”时,我没有看到任何联系人。

函数执行返回空。

我能做些什么?

谢谢。

4

1 回答 1

1

我意识到这已经过时了,因此您可能已经继续前进,但是对于其他人的发现,我相信您看不到任何联系人的原因是您没有委派给特定用户。不计算 Google Apps Premier 和 Education Edition 域上的共享联系人,联系人对该用户是私有的。

我会改变这条线

$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 = 'user@domain.com'; //whoever's contacts you want to see
$client->setAssertionCredentials($auth);

如果您查看 Google_AssertionCredentials 方法签名,您可以将前两行放在一起,但我认为这使它更加明确。根据您要实现的目标,user@domain.com 可能是从某种输入、数据库等设置的变量。

于 2014-07-22T15:27:36.250 回答