我正在研究一個腳本,以便從Google日曆中提取活動並將其放到網站上。目的是爲了能夠將我的僱主(大學圖書館)的營業時間放在我們的網站上。基本上,我們在Google日曆中創建活動,並在摘要中顯示活動時間。我的目標是以編程方式從日曆中提取相關信息,並將其放入我們主頁的div標籤中。Google Calendar API不會返回新創建的活動
從Google修改基本的PHP示例以檢索必要的信息並不困難。不幸的是,控制器代碼停止檢索新創建的事件。由於我是日曆的所有者,因此這不是權限問題。直到昨天下午,我才能夠創建新事件,並將腳本打印到我的瀏覽器中。這裏是我的代碼:
<?php
require __DIR__ . '/vendor/autoload.php';
define('APPLICATION_NAME', 'Library Hours Script');
define('CREDENTIALS_PATH', '~/path-to-credentials');
define('CLIENT_SECRET_PATH', __DIR__ . '/private key file');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-php-quickstart.json
define('SCOPES', implode(' ', array(
Google_Service_Calendar::CALENDAR_READONLY)
));
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
$client_email = '[email protected]';
$private_key = file_get_contents('/path-to-private-key');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
SCOPES,
$private_key
);
// Get the API client and construct the service object.
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Calendar($client);
// Get the calendar ID. The script currently uses the "Regular Hours" calendar.
$calendarId = '[email protected]';
$optParams = array(
'maxResults' => 1,
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print "<a href='http://www.deltastate.edu/academics/libraries/libraries-hours-of-operation/' target='_blank'>Click here for Library hours</a>.\n";
} else {
foreach ($results->getItems() as $event) {
$opHours = $event->getSummary();
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
print($opHours . " " . $start . " " . date('c'));
}
}
本質上面的代碼,使得該日曆API的請求,將結果顯1的最大數量,由當前的日期過濾事件的列表。然後它將開始日期和摘要打印到屏幕上。
API瀏覽器返回的結果與我在Web服務器上運行腳本時的結果相同。它只顯示前一段時間創建的未來事件。我昨天和今天爲本週晚些時候創建的事件不會被返回。這個問題似乎並不常見,但沒有人報道過它找到了解決方案。任何幫助將不勝感激。