【发布时间】:2014-11-07 13:51:30
【问题描述】:
我的 Intranet 上有一个 php 应用程序,它需要在我的用户日历中添加事件。
我遵循以下指示 https://developers.google.com/+/domains/authentication/delegation
我设置了 google-api-php 客户端并使用示例 service-accounts.php 作为我的起点。
$client_id = 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.apps.googleusercontent.com';
$service_account_name = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy@developer.gserviceaccount.com';
$key_file_location = 'file.p12';
$client = new Google_Client();
$client->setApplicationName("egw - intranet");
$service = new Google_Service_Calendar($client);
//authenticate
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();
$event = new Google_Service_Calendar_Event();
$event->setSummary('API V3 TEST');
$event->setLocation('rrrrrrrrrrrrrrrrrrrrrrrrr');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-11-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-11-04T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('email@domain.com');
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events- >insert('email2@domain.com', $event);
echo $createdEvent->getId();
我收到 404 not found 错误。 如果我手动与服务帐户共享我用于测试的日历,它可以工作。但我希望我的程序也能正常工作,而无需手动共享所有用户的每个日历。谁能告诉我我错过了什么?
亲切的问候,
【问题讨论】:
-
您是否在管理控制台中正确设置了范围?你应该有这个:googleapis.com/auth/calendar
-
是的,我有googleapis.com/auth/calendar 作为范围。
标签: google-calendar-api google-api-php-client