我想获得所有谷歌联系人的列表。我在Google Developers Console中创建了服务帐户,获得了ClientID,电子邮件地址和私钥。身份验证使用下一个代码完成:
require_once 'contact/google-api-php-client-master/src/Google/autoload.php';// or wherever autoload.php is located
$CLIENT_ID = '514924730309-...-fepimcc.apps.googleusercontent.com';
$SERVICE_ACCOUNT_NAME = '514924730309-...-fepimcc@developer.gserviceaccount.com';
$KEY_FILE = 'contact/key.p12';
// create client object and set app name
$client = new Google_Client();
$client->setApplicationName ("appliance-1032");
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>";
请求发送后我得到下一个回复:
Array
(
[id] => 514924730309-..-tfepimcc@developer.gserviceaccount.com
[updated] => 2015-08-09T23:02:02.040Z
[category] => Array
(
[@attributes] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://schemas.google.com/contact/2008#contact
)
)
[title] => 514924730309-..-tfepimcc@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/514924730309-..-tfepimcc%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/514924730309-..-tfepimcc%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/514924730309-..-tfepimcc%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/514924730309-..-tfepimcc%40developer.gserviceaccount.com/full?max-results=25&v=3.0
)
)
)
[author] => Array
(
[name] => (unknown)
[email] => 514924730309-..-tfepimcc@developer.gserviceaccount.com
)
[generator] => Contacts
)
但我无法在Google通讯录中找到任何我的emails
(总共约30个联系人)。我怎样才能收到电子邮件?我犯了什么错误?