自动访问谷歌日历

时间:2013-08-27 16:23:15

标签: php google-api

我需要帮助处理谷歌日历。我的代码如下:

 <?php

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/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('client id');
$client->setClientSecret('client secret');
$client->setRedirectUri('redirect uri');
$client->setDeveloperKey('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']);

  exit();
}

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();

  // Create a new event and display it.
  // Caution: every time you run this script a new event is created. Oh well.
  $event = new Google_Event();
  $event->setSummary('Appointment');
  $event->setLocation('Somewhere');
  $start = new Google_EventDateTime();
  $start->setDateTime('2013-08-24T10:00:00.000-07:00');
  $event->setStart($start);
  $end = new Google_EventDateTime();
  $end->setDateTime('2013-08-25T10:25:00.000-07:00');
  $event->setEnd($end);

  $createdEvent = $cal->events->insert('primary', $event);

  $events = $cal->events->listEvents('primary');

  echo "<pre>" . print_r($events, true) . "</pre>";
} 
else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
  exit();
}
?>

这个代码基本上有一个connect me链接,当点击时,要求用户输入他们的gmail帐户。但是,我想跳过允许此人输入用户名和密码信息的整个过程,并在运行此代码时添加事件。使用服务帐户进行谷歌api访问的想法是否解决了这个问题,还是将令牌存储在数据库中并不断改变它的工作(我正在努力做到这一点)?任何帮助将不胜感激。

0 个答案:

没有答案