我在使用 Google 提供的 PHP 库读取日历事件时遇到问题。我想要阅读的日历不是公开共享的,但我想在我的服务器应用程序上阅读它们。
尽管日历是与我共享的,但保存日历的帐户与我的帐户是分开的(我将其称为 API 帐户)。
根据 Google 上未公开共享的日历详细信息:
任何人都可以:什么也看不到
您可以: 仅查看空闲/忙碌(隐藏详细信息)。
API 帐户和我的帐户都有一个带有 P12 密钥的 OAuth2.0 服务帐户。
我遵循了以下指南:http://www.daimto.com/google_service_account_php/ http://www.daimto.com/google_service_account_php/这帮助我弄清楚了身份验证过程。我已经成功地阅读了公开共享的日历,在任何一个帐户上都没有任何问题。这是我的代码:
// Start the Google client
$client = new Google_Client();
$client->setClientId({{client_id from google}});
$client->setApplicationName("Project Name");
$client->setClientSecret({{client_secret from google}});
$key = file_get_contents({{key_file_location on my server}});
$cred = new Google_Auth_AssertionCredentials(
$Email_address,
// Scopes that we will be using
array(
"https://www.googleapis.com/auth/calendar"
),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
// Use Google Calendar API plugin
$service = new Google_Service_Calendar($client);
$optParams = array(
'singleEvents'=> true,
'timeMin'=>date(DateTime::ATOM),
'timeMax' => date(DateTime::ATOM, time()+(3 * 24 * 60 * 60))
);
$results = $service->events->listEvents(
'{{Calendar ID from Google Calendar Settings}}',
$optParams
);
但是,当尝试将代码用于未公开共享的日历时,我在任一帐户上都会收到以下错误:
致命错误:未捕获异常“Google_Service_Exception”
消息“调用 GET 时出错”https://www.googleapis.com/calendar/v3/calendars https://www.googleapis.com/calendar/v3/calendars{{日历ID}}:(404)
未找到...
当我尝试时var_dump($client->getAuth())
,我注意到有一个值可能表明身份验证不起作用:["authenticated":"Google_Client":private]=> bool(false)
。这会发生在两个帐户以及公开共享和公开未共享的日历上。
我可以通过我的帐户使用 Google API Explorer 在未公开共享的日历上显示 JSON 数据。 Google PHP API 或 Google 帐户设置是否缺少某些内容,无法让我做我想做的事情?
感谢您的时间。