【发布时间】:2018-12-21 15:59:01
【问题描述】:
我有一个小 php 脚本工作了几个月,它可以通过 Google Calendar API 创建和更新 Google Calendar 事件。它工作了 2 个月,然后突然停止工作,尽管我找不到任何变化。
如果我将 calendarId 更改为“主要”(这是服务帐户拥有的唯一真正的日历),代码仍然有效。所以我知道代码本身在创建事件方面起作用,只是无法访问共享日历以在这些日历上创建事件。以下是我最初设置代码的方式以及我尝试过的方式:
1) 我创建了服务帐户并授予它“所有者”角色。然后我创建了密钥。 (请注意,这是一个 GSuite 帐户,我在其中启用了“启用 G Suite 域范围委派”)。
2) 对于我需要使用该帐户管理的每个日历,我进入了日历共享设置(“与特定人员共享”)并添加了具有“进行更改和管理共享”权限的服务帐户“电子邮件”。
3) PHP 脚本也非常基础,使用最新版本的 Google API PHP 客户端库。它进行身份验证,然后可以创建一个事件(请注意,一些细节已更改,例如我的应用名称以掩盖一些细节)。
require __DIR__ . '/vendor/autoload.php';
// Got the data and ready to authenticate
$client = new Google_Client();
$client->setApplicationName("MyAppName");
$client->setAuthConfig('credentials.json');
$client->setScopes([Google_Service_Calendar::CALENDAR_READONLY,Google_Service_Calendar::CALENDAR]);
$service = new Google_Service_Calendar($client);
// GATHER ALL PASSED IN DATA INTO VARIABLES
$calendarId = 'mycalendar@group.calendar.google.com';
$summary = "Test calendar entry";
$startDate = "2019-02-19";
$endDate = "2019-02-19";
$attendees = "user1@domain.com,user2@domain.com";
//Construct event array to send
$eventArray = array(
'summary' => $summary,
'start' => array(
'date' => $startDate,
'timeZone' => 'America/Chicago',
),
'end' => array(
'date' => $endDate,
'timeZone' => 'America/Chicago',
),
'guestsCanInviteOthers' => true,
'guestsCanModify' => true,
'guestsCanSeeOtherGuests' => true
);
//Only include attendees in event array if we find them
if ($data["attendees"]) {
// Attendees are present
$attendees = explode(",", $data["attendees"]);
$attendeesArray = array();
foreach ($attendees as $key => $value) {
$attendeesArray[$key]['email'] = $value;
};
//Now add to original event array
$eventArray["attendees"] = $attendeesArray;
}
//Create the event and return event id
$event = new Google_Service_Calendar_Event($eventArray);
$event = $service->events->insert($calendarId, $event);
$eventId = $event->id;
$createdDate = $event->created;
这可以进行身份验证,但是一旦您开始使用“主要”以外的日历,它就会出现错误:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Not Found"
}
],
"code": 404,
"message": "Not Found"
}
}
我通过谷歌搜索,查看了该网站上的许多教程和建议,我尝试过的所有操作,包括完全重置服务帐户都没有解决问题。
【问题讨论】: