【发布时间】:2020-02-15 00:13:15
【问题描述】:
我在使用 Google 提供的 PHP 库读取日历事件时遇到问题。我想阅读的日历不是公开共享的,但我想在我的服务器应用程序上阅读它们。
保存日历的帐户与我的帐户是分开的(我将其称为 API 帐户),尽管日历是与我共享的。
根据 Google 上未公开共享的日历详细信息:
任何人都可以:什么都看不见
您可以:仅查看忙/闲(隐藏详细信息)。
API 帐户和我的帐户都有一个带有 P12 密钥的 OAuth2.0 服务帐户。
我遵循了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”与 message '调用 GET 时出错 https://www.googleapis.com/calendar/v3/calendars{{CalendarID}}:(404) 未找到...
当我尝试var_dump($client->getAuth()) 时,我注意到有一个值可能表明身份验证不起作用:["authenticated":"Google_Client":private]=> bool(false)。这发生在两个帐户以及公开共享和未公开共享的日历上。
我可以通过我的帐户使用 Google API Explorer 在未公开共享的日历上显示 JSON 数据。 Google PHP API 或 Google 帐户设置是否缺少某些东西可以让我做我想做的事?
感谢您的宝贵时间。
【问题讨论】:
标签: php api google-calendar-api shared