3
votes

When I use "this solution" I get a blank contacts array.

I'm using "google-api-php-client-0.6.7" with a serviceAccount.php into the examples/contacts folder, and a Google_ContactsService into src/contrib folder.

I have configured the Google Apis Access, with OAUTH 2 as Server Account. I have the p12 file, a client id and a cliend mail address. The project name is "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;
    }
}

When I go to "http://server.com/packs/googleapi/examples/contacts/serviceAccount.php" I don't see any contact.

The function Execute return empty.

What can I do?

Thanks.

1
Thanks for your code. helped my situation :)DevZer0

1 Answers

1
votes

I realize this is old so you have probably moved on but for other's finding this I believe the reason you don't see any contacts is that you are not delegating to a specific user. Not counting shared contacts on Google Apps Premier and Education Editions domains, contacts are private to that user.

I would change this line

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

to be more like this

$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);

You could keep the first two lines together if you look at Google_AssertionCredentials method signature, but I think this makes it more explicit. Depending on what you are trying to achieve, [email protected] will likely be a variable that is set from some kind of input, database, etc.