我们一直在使用谷歌日历来安排一些任务,我想获取日历上的数据,以便在我们的内部网站上使用。
我使用Google API控制台创建了API密钥和客户端ID、客户端机密等,并启用了对日历API的访问。。现在,我应该使用什么PHP代码通过REST请求日历数据?
到目前为止,我使用谷歌的PHP客户端库拼凑出了以下内容
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/google-api-php-client/src/Google_Client.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('My Application Name');
$client->setClientId('my_client_id');
$client->setClientSecret('my_client_secret');
$client->setRedirectUri('my_oauth2_callback');
$client->setDeveloperKey('my_developer_key');
$cal = new Google_CalendarService($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
// If the user has been redirected back to our page with an authorization code, exchange the code for an access token.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
// Here's where I need to do something to get the calendar data
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a href='$authUrl'>Connect Me!</a>";
}
我希望能够跳过"连接我!"授权步骤。访问该应用程序的用户可能无法访问谷歌中的共享日历,但仍应能够在此应用程序中查看日历中的数据。
我的服务器上还安装了Zend Framework。我更喜欢不需要Zend的解决方案,但如果它有效,我会使用它。
我通过阅读PHP客户端库的源文档找到了问题的答案。特别是/contrib/Google_CalendarService.php