简体   繁体   English

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

[英]Google Contact Api PHP - Server Account (server to server): Empty contacts array

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. 我将带有serviceAccount.php的“google-api-php-client-0.6.7”用于examples / contacts文件夹,将一个Google_ContactsService用于src / contrib文件夹。

I have configured the Google Apis Access, with OAUTH 2 as Server Account. 我配置了Google Apis Access,OAUTH 2作为服务器帐户。 I have the p12 file, a client id and a cliend mail address. 我有p12文件,客户端ID和cliend邮件地址。 The project name is "Google Contacts Sample". 项目名称为“Google Contacts Sample”。

serviceAccount.php: 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 = '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: 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. 当我转到“ http://server.com/packs/googleapi/examples/contacts/serviceAccount.php ”时,我看不到任何联系。

The function Execute return empty. 函数Exec​​ute返回空。

What can I do? 我能做什么?

Thanks. 谢谢。

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. 不计算Google Apps专业版和教育版域上的共享联系人,联系人是该用户的私密联系人。

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 = 'user@domain.com'; //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. 如果你查看Google_AssertionCredentials方法签名,你可以将前两行保持在一起,但我认为这使它更明确。 Depending on what you are trying to achieve, user@domain.com will likely be a variable that is set from some kind of input, database, etc. 根据您要实现的目标,user@domain.com可能是一个由某种输入,数据库等设置的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM