【发布时间】:2014-05-30 14:51:48
【问题描述】:
我已经把头撞到墙上几个小时了,我试图在整个互联网上搜索但没有成功。我的问题是我无法使用 Google API PHP 客户端(从 GitHub 下载)让域范围的委派工作。我假设身份验证正在工作,因为如果日历已共享到服务帐户的电子邮件 (xxx@@developer.gserviceaccount.com),我就可以读取日历。
我希望能够读取和写入我们域 (Google Apps for Business) 中的所有用户日历而无需与服务帐户共享单个日历。如果我将我的(或其他人的)日历共享到服务帐户的电子邮件 (xxx@@developer.gserviceaccount.com),我就可以阅读日历。
这就是我所做的:
- 我在开发者控制台中创建了一个项目。
- 我创建了一个服务帐户并下载了 .p12 文件。
- 我访问了 admin.google.com 并将新创建的客户端 ID 添加到安全性 --> 高级设置 --> 管理 OAuth 客户端访问 - 我将新创建的客户端 ID 添加为客户端名称并添加了 https://www.google.com/calendar/feeds/作为范围。之后我点击了“授权”。
- 已下载最新的 Google API PHP 客户端。
- 修改了包含的示例 (service-account.php) 以匹配我想要的。
- 运行脚本时,我收到“404 Not found”。
- 在我将日历(通过 Gmail --> 日历)共享到服务帐户的电子邮件后,它开始工作。
代码如下:
<?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;
}
}
?>
所以现在我正在寻找可以帮助我解决这个问题的天才。我将不胜感激,甚至愿意为能够使其工作的人捐款。
【问题讨论】:
-
我了解您不希望通过共享代码路径,但是否可以选择为每个人获取 OAuth2 令牌,然后以这种方式更新数据?
-
嗨,卢克!不幸的是,这是我不想做的事情。如果我无法找到更好的解决方案,那么我必须弯腰,但希望有人知道如何解决这个问题,因为我想让用户的日历管理尽可能简单。
-
我对这里可能发生的事情很感兴趣,我也需要创建一个日历以与所有其他人共享或写入所有日历某些事件。想知道是否有人对此有解决方案,顺便说一句我无法使用 Google Apps for Business
标签: php api calendar google-calendar-api google-api-php-client