Search code examples
phpgoogle-api-php-clientgoogle-contacts-apiservice-accounts

Display emails - Google Contact API


I create all of needed to get email for google contact. See my video http://screencast.com/t/3j3elQtu

I can't find where i done mistake.I pass authorization and get response, but i this response i can't find any emails address of my contacts

session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL); 

require_once 'contact/google-api-php-client-master/src/Google/autoload.php';// or wherever autoload.php is located

$CLIENT_ID = '1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8.apps.googleusercontent.com';
$SERVICE_ACCOUNT_NAME = '1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8@developer.gserviceaccount.com';
$KEY_FILE = 'contact/key/test.p12';

// create client object and set app name
$client = new Google_Client();
$client->setApplicationName ("helical-theater-103318");

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

// set assertion credentials
$client->setAssertionCredentials(
    new Google_Auth_AssertionCredentials(
                                            $SERVICE_ACCOUNT_NAME, 
                                            array('https://www.googleapis.com/auth/contacts.readonly'), 
                                            file_get_contents($KEY_FILE),
                                            'notasecret',
                                            'http://oauth.net/grant_type/jwt/1.0/bearer'

    )
);

// other settings
$client->setClientId($CLIENT_ID);

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

$max_results = 1000;
$token = $client->getAccessToken();

// create service and get data
$req = new Google_Http_Request('https://www.google.com/m8/feeds/contacts/default/full?v=3.0');

$val = $client->getAuth()->authenticatedRequest($req);

$response = json_encode(simplexml_load_string($val->getResponseBody()));
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";

echo '--------------------------------------------------------------------';

echo '<pre>';
var_dump($val);
echo '</pre>';

After request send i get next response:

Array
(
    [id] => 1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8@developer.gserviceaccount.com
    [updated] => 2015-08-10T18:39:37.529Z
    [category] => Array
        (
            [@attributes] => Array
                (
                    [scheme] => http://schemas.google.com/g/2005#kind
                    [term] => http://schemas.google.com/contact/2008#contact
                )

        )

    [title] => 1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8@developer.gserviceaccount.com's Contacts
    [link] => Array
        (
            [0] => Array
                (
                    [@attributes] => Array
                        (
                            [rel] => alternate
                            [type] => text/html
                            [href] => https://www.google.com/
                        )

                )

            [1] => Array
                (
                    [@attributes] => Array
                        (
                            [rel] => http://schemas.google.com/g/2005#feed
                            [type] => application/atom+xml
                            [href] => https://www.google.com/m8/feeds/contacts/1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8%40developer.gserviceaccount.com/full?v=3.0
                        )

                )

            [2] => Array
                (
                    [@attributes] => Array
                        (
                            [rel] => http://schemas.google.com/g/2005#post
                            [type] => application/atom+xml
                            [href] => https://www.google.com/m8/feeds/contacts/1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8%40developer.gserviceaccount.com/full?v=3.0
                        )

                )

            [3] => Array
                (
                    [@attributes] => Array
                        (
                            [rel] => http://schemas.google.com/g/2005#batch
                            [type] => application/atom+xml
                            [href] => https://www.google.com/m8/feeds/contacts/1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8%40developer.gserviceaccount.com/full/batch?v=3.0
                        )

                )

            [4] => Array
                (
                    [@attributes] => Array
                        (
                            [rel] => self
                            [type] => application/atom+xml
                            [href] => https://www.google.com/m8/feeds/contacts/1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8%40developer.gserviceaccount.com/full?max-results=25&v=3.0
                        )

                )

        )

    [author] => Array
        (
            [name] => (unknown)
            [email] => 1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8@developer.gserviceaccount.com
        )

    [generator] => Contacts
)

--------------------------------------------------------------------

object(Google_Http_Request)#7 (15) {
  ["batchHeaders":"Google_Http_Request":private]=>
  array(3) {
    ["Content-Type"]=>
    string(16) "application/http"
    ["Content-Transfer-Encoding"]=>
    string(6) "binary"
    ["MIME-Version"]=>
    string(3) "1.0"
  }
  ["queryParams":protected]=>
  array(1) {
    ["v"]=>
    string(3) "3.0"
  }
  ["requestMethod":protected]=>
  string(3) "GET"
  ["requestHeaders":protected]=>
  array(1) {
    ["authorization"]=>
    string(80) "Bearer ya29.ywFkKGJ5BvRTMBTCj3SA-8AU9HluYvgQhad1DNkgRbFXc_GoTnWenFd6hDTdMNLOfPKZ"
  }
  ["baseComponent":protected]=>
  string(22) "https://www.google.com"
  ["path":protected]=>
  string(31) "/m8/feeds/contacts/default/full"
  ["postBody":protected]=>
  NULL
  ["userAgent":protected]=>
  NULL
  ["canGzip":protected]=>
  NULL
  ["responseHttpCode":protected]=>
  int(200)
  ["responseHeaders":protected]=>
  array(12) {
    ["expires"]=>
    string(29) "Mon, 10 Aug 2015 18:39:37 GMT"
    ["date"]=>
    string(29) "Mon, 10 Aug 2015 18:39:37 GMT"
    ["cache-control"]=>
    string(49) "private, max-age=0, must-revalidate, no-transform"
    ["etag"]=>
    string(30) ""Rns5cTVSLyt7I2A9XRVaEk4LTgM.""
    ["vary"]=>
    string(15) "Origin
X-Origin"
    ["content-type"]=>
    string(35) "application/atom+xml; charset=UTF-8"
    ["x-content-type-options"]=>
    string(7) "nosniff"
    ["x-frame-options"]=>
    string(10) "SAMEORIGIN"
    ["x-xss-protection"]=>
    string(13) "1; mode=block"
    ["content-length"]=>
    string(4) "2024"
    ["server"]=>
    string(3) "GSE"
    ["alternate-protocol"]=>
    string(12) "443:quic,p=1"
  }
  ["responseBody":protected]=>
  string(2024) "

 1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8@developer.gserviceaccount.com
 2015-08-10T18:39:37.529Z








  (unknown)
  1023630033458-tcblmt4it6jft9rfkko7474l4dmj4sf8@developer.gserviceaccount.com

 Contacts
 0
 1
 25

"
  ["expectedClass":protected]=>
  NULL
  ["expectedRaw":protected]=>
  bool(false)
  ["accessKey"]=>
  NULL
}

How can i get my contact emails? and where i made a mistake? (I am owner of permission.)


Solution

  • This is solution:

    Your code:

    $response = json_encode(simplexml_load_string($val->getResponseBody()));
    print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";
    

    Change to:

    $xml = simplexml_load_string($val->getResponseBody());
                $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');         
    
                $output_array = array();
                foreach ($xml->entry as $entry) {
                  foreach ($entry->xpath('gd:email') as $email) {
                    $output_array[] = array((string)$entry->title, (string)$email->attributes()->address);
                  }
                }
               print_r($output_array);