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

时间:2015-01-29 16:23:42

标签: php calendar google-api google-calendar-api google-api-v3

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

但我接下来要做的就是删除Google日历的全部内容。最初我以为我会通过阅读所有事件然后删除每个事件来做到这一点,但根据Google我可以在一个命令中执行此操作:

$服务 - > calendars->清除($ CAL_ID);

但是,由于Google API文档仅涵盖实际命令,并且未显示任何需要在此之前的代码,因此我使用了在事件插入脚本中工作的授权代码,但我无法获得它为清理数据而工作,我收到了错误:

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

我的整个代码如下:

<?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';
}
?>

1 个答案:

答案 0 :(得分:1)

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

我无法获得明确的命令,所以我必须这样做: 1.阅读日历中的所有事件 2.删除每一个

首先确保您的Google日历设置正确,以允许任何此类内容工作 - 此处的答案中详细介绍了4个简单步骤Inserting Google Calendar Entries with Service Account

接下来,这是我使用的代码 - 请注意,最后2个对xxxxxxxxxxxx@googlemail.com的引用是日历所有者电子邮件地址 - 删除不适用于'primary'或$ 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.');
    }
}
?>