无法使用服务帐户访问通过API创建的新Google日历

时间:2020-05-28 13:00:21

标签: google-calendar-api

我们正在使用Google Calendar API创建日历并代表用户进行管理。

为此,我们使用以下ACL规则创建日历:

$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig('/.../oauth_secret.json');
$client->setRedirectUri(REDIRECT_URI);
$client->authenticate($_GET['code']);
$accessToken = $client->getAccessToken();
$client->setAccessToken($accessToken);
$service = new Google_Service_Calendar($client);

$calendar = new Google_Service_Calendar_Calendar();
$calendar->setSummary($calendarName);
$calendar->setTimeZone('Europe/Paris');
$newCalendar = $service->calendars->insert($calendar);
$calendarId = $newCalendar->getId();

$rule = new Google_Service_Calendar_AclRule();
$scope = new Google_Service_Calendar_AclRuleScope();
$scope->setType("user");
$scope->setValue("295093256...012ulkk4oklusjh8...@developer.gserviceaccount.com");
$rule->setScope($scope);
$rule->setRole("owner");
$service->acl->insert($calendarId, $rule);

因此,我们尝试像这样访问日历:

$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig('/.../private_key.json');
if ($client->isAccessTokenExpired()) $client->refreshTokenWithAssertion();

$this->service = new Google_Service_Calendar(client);

$calendar = $service->calendarList->get($calendarId);

直到几个月前,它一直运行良好,但现在我们收到此错误:

google_runner error: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
   }
  ],
  "code": 404,
  "message": "Not Found"
 }
}

它仍然适用于以前创建的日历,但不适用于新的日历。我正在寻求帮助,因为我找不到原因。

请注意,您可以在此处在新日历上正确创建ACL:

enter image description here

有什么主意吗?

1 个答案:

答案 0 :(得分:1)

找到了!

创建日历时,Google不会再自动创建用户日历列表的条目。

要检查日历是否存在,我只需要替换:

$calendar = $service->calendarList->get($calendarId);

通过

$calendar = $service->calendars->get($calendarId);