След часове четене на документация за API на Google и търсене в мрежата успях да напиша проста PHP функция за вмъкване на събитие в моя календар в Google (ако някой иска кода, просто попитайте).
Но следващото нещо, което искам да направя, е да изтрия цялото съдържание на моя календар в Google. Първоначално мислех, че ще направя това, като прочета всички събития и след това изтрия всяко едно, но според Google мога да направя това с една команда:
$service->calendars->clear($cal_id);
Въпреки това, тъй като документацията на Google API обхваща само действителната команда и не показва нито един от кода, който трябва да предхожда това, така че използвах кода за оторизация, който работи в скрипта за вмъкване на събитие, но просто не мога да го накарам да работи за изчиствам данните и получавам грешка:
Забележка: Недефинирана променлива: услуга в index.php на ред 68
Целият ми код следва:
<?php
//
// example code taken from:
// https://developers.google.com/google-apps/calendar/v3/reference/calendars/clear
//
//
calendclear('[email protected]');
//---------------------------------------------------//
// funtion to delete all events from Google calendar //
//---------------------------------------------------//
function calendclear ($cal_id) {
session_start();
require_once '../google-api-php-client-master/autoload.php';
//Google credentials
$client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$service_account_name = '[email protected]';
$key_file_location = '../google-api-php-client-master/API Project-xxxxxxxxxxxx.p12';
if (!strlen($service_account_name) || !strlen($key_file_location))
echo missingServiceAccountDetailsWarning();
$client = new Google_Client();
$client->setApplicationName("Whatever the name of your app is");
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/calendar'),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
try {
$client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
$_SESSION['service_token'] = $client->getAccessToken();
$calendarService = new Google_Service_Calendar($client);
$calendarList = $calendarService->calendarList;
//delete all calendar data
try {
$service->calendars->clear($cal_id);
} catch (Exception $e) {
var_dump($e->getMessage());
}
echo 'Calendar Successfully Cleared';
}
?>