我们一直在使用谷歌日历来安排一些任务,我想在日历上获取数据以在我们的内部网站上使用。
我已经使用Google API 控制台创建了 API 密钥和客户端 ID、客户端密码等,并且启用了对日历 API 的访问。现在我应该使用什么 PHP 代码通过 REST 请求日历数据?
到目前为止,这是我使用Google 的 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>";
}
我希望能够跳过“连接我!” 授权步骤。访问应用程序的用户可能无法访问 Google 中的共享日历,但仍应能够查看此应用程序中日历中的数据。
我的服务器上也安装了 Zend 框架。我更喜欢不需要 Zend 的解决方案,但如果它有效,我会使用它..