首页 文章

使用相同设置访问其他日历 - Google Calendar API PHP

提问于
浏览
1

我使用Google Calendar PHP API从日历中获取活动 .

一切都适用于初始日历,但现在我尝试使用相同的设置从另一个日历(相同的Google帐户)接收事件 .

日志中出现以下错误:

PHP致命错误:未捕获异常'Google_Service_Exception',并在/www/apache/domains/www.domain.ee/htdocs/wp-content/themes/THEME/booking/vendor/google中显示错误'错误呼叫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)未找到' /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会准确返回我要查找的事件 .

1 回答

  • 1

    从Google Calendar API中的处理API错误中,您获得的error 404意味着找不到指定的资源 . 其他可能的原因是所请求的资源(具有提供的ID)从未存在或访问用户无法访问的日历 .

    这里建议的操作是使用exponential backoff .

    基于此SO question,此问题的另一个原因是当使用service account访问私人日历时,如果您拥有包含这些日历的域,则需要执行权限委派,或者您需要与该电子邮件地址共享私人日历 . 服务帐户 .

相关问题