使用相同的设置访问另一个日历-谷歌日历API PHP


Access another calendar using same settings - Google Calendar API PHP

我正在使用Google calendar PHP API从日历中获取事件。

一切工作与初始日历,但现在我试图使用相同的设置从另一个日历接收事件(相同的谷歌帐户)。

日志中有以下错误:

PHP致命错误:未捕获的异常'Google_Service_Exception'与消息'错误调用GET https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID.calendar.google.com/events?maxResults=999&orderBy=startTime&singleEvents=true&timeZone=Europe%2FTallinn&timeMin=2016-09-19T00%3A01%3A00Z&timeMax=2018-07-17T00%3A00%3A00Z:(404) Not Found' in/www/apache/domains/www.domain.ee/htdocs/wp-content/themes/THEME/booking/vendor/google/apiclient/src/Google/Http/REST.php:79

这里是代码:(我唯一改变的是$calendarId)

require_once 'vendor/autoload.php';
$client_email = 'name@name.iam.gserviceaccount.com';
$private_key = file_get_contents('name.p12');
$scopes = array('https://www.googleapis.com/auth/calendar');
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Calendar($client);
// Print the next 999 events on the user's calendar.
$calendarId = 'calendar_id@group.calendar.google.com';
$optParams = array(
  'maxResults' => 999,
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeZone' => 'Europe/Tallinn',
  'timeMin' => '2016-09-19T00:01:00Z',
  'timeMax' => '2018-07-17T00:00:00Z',
);
$results = $service->events->listEvents($calendarId, $optParams);

将相同的数据插入到Google api Explorer返回我正在寻找的事件。

从Google Calendar API中的Handling API Errors中,您获得的error 404有时意味着没有找到指定的资源。其他可能的原因是当请求的资源(具有提供的ID)从未存在或访问用户无法访问的日历时。

此处建议的操作是使用指数回退。

根据这个SO问题,这个问题的另一个原因是当使用服务帐户访问私有日历时,如果您拥有包含这些日历的域,则需要执行权限委托,或者您需要与服务帐户的电子邮件地址共享私有日历。