如何使用api从具有客户端令牌的服务器将事件插入到Google日历中

时间:2016-04-14 05:31:34

标签: php api

我正在使用以下代码,但我无法在谷歌日历中插入事件。请帮帮我。如果您有任何信息。请尽快回复。

$client = new Google_Client();
$client->setApplicationName("calender");
$client->setClientId('1065799091955-kuek7e5miai4q590v49jpntr00u3pcbp.apps.googleusercontent.com');
$client->setClientSecret('SBOs_gfbFn92bpGdBt8aQY1z');
$client->setRedirectUri('http://www.sydneywebexperts.com.au/Add-Calendar/event.php');
$client->setDeveloperKey('AIzaSyDjRKaWPdAlwvJQlKeLuxKnyzXdKZiR5_A');

//$cal = new Google_CalendarService($client);

$event = new Google_CalendarService(array(
  'summary' => 'Google I/O 2015',
  'location' => '800 Howard St., San Francisco, CA 94103',
  'description' => 'A chance to hear more about Google\'s developer products.',
  'start' => array(
    'dateTime' => '2015-05-28T09:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'end' => array(
    'dateTime' => '2015-05-28T17:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2'
  ),
  'attendees' => array(
    array('email' => 'lpage@example.com'),
    array('email' => 'sbrin@example.com'),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));

$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);

 ----------------------  or------------------------

<?php
error_reporting(E_ALL);
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();


if ((isset($_SESSION)) && (!empty($_SESSION))) {
   echo "There are cookies<br>";
   echo "<pre>";
   print_r($_SESSION);

}

$client = new Google_Client();
$client->setApplicationName("calender");
$client->setClientId('1065799091955-kuek7e5miai4q590v49jpntr00u3pcbp.apps.googleusercontent.com');
$client->setClientSecret('SBOs_gfbFn92bpGdBt8aQY1z');
$client->setRedirectUri('http://www.sydneywebexperts.com.au/Add-Calendar/event.php');
$client->setDeveloperKey('AIzaSyDjRKaWPdAlwvJQlKeLuxKnyzXdKZiR5_A');

$cal = new Google_CalendarService($client);

if (isset($_GET['logout'])) {
  echo "<br><br><font size=+2>Logging out</font>";
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  echo "<br>I got a code from Google = ".$_GET['code']; // You won't see this if redirected later
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
  echo "<br>I got the token = ".$_SESSION['token']; // <-- not needed to get here unless location uncommented
}

if (isset($_SESSION['token'])) {
  echo "<br>Getting access";
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()){

  echo "<hr><font size=+1>I have access to your calendar</font>";
  $event = new Google_Event();
  $event->setSummary('Halloween');
  $event->setLocation('The Neighbourhood');
  $start = new Google_EventDateTime();
  $start->setDateTime('2016-4-13T10:00:00.000-05:00');
  $event->setStart($start);
  $end = new Google_EventDateTime();
  $end->setDateTime('2016-4-13T10:25:00.000-05:00');
  $event->setEnd($end);
  print_r($event);
  $createdEvent = $cal->events->insert('primary', $event);
  print_r($createdEvent);
  echo "<br><font size=+1>Event created</font>";
  echo "<hr><br><font size=+1>Already connected</font> (No need to login)";

} else {

  $authUrl = $client->createAuthUrl();
  print "<hr><br><font size=+2><a href='$authUrl'>Connect Me!</a></font>";

}

$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
echo "<br><br><font size=+2><a href=$url?logout>Logout</a></font>";

?>

我很沮丧,但我无法在日历中插入事件。

0 个答案:

没有答案