Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google联系人Api PHP-服务器帐户(服务器到服务器):空联系人数组_Php_Google Api_Google Contacts Api - Fatal编程技术网

Google联系人Api PHP-服务器帐户(服务器到服务器):空联系人数组

Google联系人Api PHP-服务器帐户(服务器到服务器):空联系人数组,php,google-api,google-contacts-api,Php,Google Api,Google Contacts Api,当我使用“此解决方案”时,我会得到一个空白联系人数组 我使用“google-api-php-client-0.6.7”,将servicecomport.php放入examples/contacts文件夹,将google_contacts服务放入src/contrib文件夹 我已经配置了GoogleAPI访问,使用OAuth2作为服务器帐户。我有p12文件、客户端id和客户端邮件地址。项目名称为“谷歌联系人样本” servicecomport.php: <?php require_once

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

我使用“google-api-php-client-0.6.7”,将servicecomport.php放入examples/contacts文件夹,将google_contacts服务放入src/contrib文件夹

我已经配置了GoogleAPI访问,使用OAuth2作为服务器帐户。我有p12文件、客户端id和客户端邮件地址。项目名称为“谷歌联系人样本”

servicecomport.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>';
$client->setAssertionCredentials(new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.googleapis.com/auth/contacts'), $key));

我意识到这已经过时了,所以你可能已经离开了,但对于其他人的发现,我认为你没有看到任何联系人的原因是你没有委托给特定的用户。不包括Google Apps Premier和Education Edition域上的共享联系人,联系人对该用户是私有的

我想换一条线

$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可能是通过某种输入、数据库等设置的变量。

谢谢您的代码。帮助了我的处境:)
$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);