1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?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('xxxxxx');
$client->setClientId('xxxxxxxx');
$client->setClientSecret('xxxxxxxx');
$client->setRedirectUri('http://tonsite/cette_page.php');
$client->setScopes('http://www.google.com/calendar/feeds/');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
$cal = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary('Lire la doc');
$event->setLocation('Maison');
$start = new Google_EventDateTime();
$start->setDateTime('2013-02-20T07:00:00.000+01:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-02-20T22:00:00.000+01:00');
$event->setEnd($end);
try {
$createdEvent = $cal->events->insert('NOM_DU_CALENDRIER', $event);
echo 'Statut : ' . $createdEvent['status'];
}
catch (Exception $e) {
echo 'Erreur : ', $e->getMessage(), "\n";
}
} else {
$authUrl = header('Location: ' . $client->createAuthUrl());
} |