gpt4 book ai didi

php - Google Contact Api PHP - 服务器帐户(服务器到服务器): Empty contacts array

转载 作者:搜寻专家 更新时间:2023-10-31 21:35:55 27 4
gpt4 key购买 nike

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

我在 examples/contacts 文件夹中使用“google-api-php-client-0.6.7”和 serviceAccount.php,在 src/contrib 文件夹中使用 Google_ContactsService。

我已经配置了 Google Apis Access,使用 OAUTH 2 作为服务器帐户。我有 p12 文件、一个客户端 ID 和一个客户端邮件地址。项目名称是“Google 联系人示例”。

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:

<?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;
}
}

当我转到“http://server.com/packs/googleapi/examples/contacts/serviceAccount.php”时,我没有看到任何联系人。

Execute 函数返回空。

我能做什么?

谢谢。

最佳答案

我意识到这是旧的,所以您可能已经继续前进,但对于其他人的发现,我相信您看不到任何联系人的原因是您没有委派给特定用户。不包括 Google Apps Premier 和 Education Edition 域中的共享联系人,联系人是该用户的私有(private)联系人。

我会改变这一行

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

更像这样

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

关于php - Google Contact Api PHP - 服务器帐户(服务器到服务器): Empty contacts array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19757918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com