Google Calendar API登录

时间:2014-02-18 08:40:12

标签: php google-api google-calendar-api

我是谷歌日历API的新手。 现在,我通过Google API将我的网站连接到我的Google日历,问题是即使我登录到我的Google帐户后仍然有“连接我!”出现的东西。 请帮忙。

require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('my_oauth2_client_id');
$client->setClientSecret('my_oauth2_client_secret');
$client->setRedirectUri('my_oauth2_redirect_uri'); 
$client->setDeveloperKey('my_developer_key');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  $calList = $cal->calendarList->listCalendarList();
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";


$_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

0 个答案:

没有答案