我现在已经在墙上撞了几个小时,我试图在互联网上搜索但没有成功。我的问题是我无法使用Google API PHP客户端(从GitHub下载)使域范围的授权工作。我假设身份验证正在运行,因为如果日历已经分享到服务帐户的电子邮件(xxx @@ developer.gserviceaccount.com),我就能阅读日历。
我希望能够读取和写入我们域中的所有用户日历(Google Apps for Business),无需必须将各个日历共享到服务帐户。如果我将我(或其他人的)日历分享到服务帐户的电子邮件(xxx @@ developer.gserviceaccount.com),我就可以阅读日历。
这就是我所做的:
以下是代码:
<?php
session_start();
include_once "templates/base.php";
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
$client_id = 'xxxx.apps.googleusercontent.com';
$service_account_name = 'xxxx@developer.gserviceaccount.com';
$key_file_location = '/home/LALALA/public_html/xxxx-privatekey.p12';
echo pageHeader("Service Account Access");
if ($client_id == '<CLIENT_ID>'
|| !strlen($service_account_name)
|| !strlen($key_file_location)) {
echo missingServiceAccountDetailsWarning();
}
$client = new Google_Client();
$client->setApplicationName("Calendar");
$client->setClientId($client_id);
$client->setClientSecret("notasecret");
//$bookService = new Google_Service_Books($client);
$service = new Google_Service_Calendar($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/calendar'),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$events = $service->events->listEvents('some.email@our.domain.com');
while(true) {
foreach ($events->getItems() as $event) {
echo $event->getSummary()."<br />";
}
$pageToken = $events->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$events = $service->events->listEvents('primary', $optParams);
} else {
break;
}
}
?>
所以现在我正在寻找能帮助我解决这个问题的天才。我会非常感激,甚至愿意为能够让它发挥作用的人捐出一小笔捐款。
答案 0 :(得分:0)
试试这个:
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,,
array('https://www.googleapis.com/auth/calendar'),
$key,
'notasecret',
'http://oauth.net/grant_type/jwt/1.0/bearer',
'some.email@our.domain.com'
);
告诉Google您要模拟的人,然后您就可以为此用户操作Google日历的所有内容。