如何使用 API V3 PHP 读取事件并删除 Google 日历中的所有事件


How to read events and delete all events in Google Calendar using API V3 PHP

经过数小时的阅读Google API文档和搜索网络,我设法编写了一个简单的PHP函数,将事件插入我的Google日历(如果有人想要代码,请询问)。

但是,我想做的下一件事是删除Google日历的全部内容。 最初我以为我会通过读取所有事件然后删除每个事件来做到这一点,但根据 Google 的说法,我可以在单个命令中做到这一点:

$service->calendars->clear($cal_id);

但是,由于Google API文档仅涵盖实际命令,并且没有显示任何需要在此之前的代码,因此我使用了在事件插入脚本中起作用的授权代码,但是我无法让它用于清除数据,并且出现错误:

注意:未定义的变量:索引中的服务.php在第 68 行

我的整个代码如下:

<?php
//
// example code taken from:
// https://developers.google.com/google-apps/calendar/v3/reference/calendars/clear
//
//
calendclear('xxxxxxxxxxx@googlemail.com');
//---------------------------------------------------//
// 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 = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
    $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';
}
?>

经过多次试验和错误,我找到了一种使用 API v3 清除 Google 日历的方法。

无法获得明确的命令来工作,所以这样做我不得不:1. 读取日历中的所有事件2. 删除每个

首先,确保您的Google日历设置正确,以允许任何这些东西工作 - 答案中有4个简单的步骤 使用服务帐户插入Google日历条目

接下来,这是我使用的代码 - 请注意,最后 2 个对 xxxxxxxxxxxx@googlemail.com 引用是日历所有者电子邮件地址 - 删除不适用于"主要"或 $service_account_name。

如果你正在玩这些东西,我希望这有所帮助,因为我生命的最后两天我试图解决这个问题并没有完全浪费:)

<?php
session_start();
require_once '../google-api-php-client-master/autoload.php';
//Google credentials
$client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$service_account_name = 'xxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
$key_file_location = '../google-api-php-client-master/API Project-xxxxxxxxxxx.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();
/* ------------------------- We are now properly authenticated ------------------- */
$cal = new Google_Service_Calendar($client);
$event_list = $cal->events->listEvents('xxxxxxxxxxxx@googlemail.com');
$events = $event_list->getItems();
// loop through all events read from calendar
foreach ($events as $event)
{
    $myEvent=$event->getId();
    $mySummary=$event->getSummary();
    $MyStart=$event->getStart()->getDateTime();
    $MyEnd=$event->getEnd()->getDateTime();
    echo 'about to delete event '.$mySummary.' ID: ('.$myEvent.')<br />';
    // now delete the event found
    try {
      $cal->events->delete('xxxxxxxxxxxx@googlemail.com', $myEvent);
      echo 'Success.<br />';
    } catch (Exception $e) {
      var_dump($e->getMessage());
      die('Event Delete Failed.');
    }
}
?>