Google calendar api 如何使用API V3 PHP清除Google日历中的所有事件

Google calendar api 如何使用API V3 PHP清除Google日历中的所有事件,google-calendar-api,google-api-php-client,Google Calendar Api,Google Api Php Client,我花了很长时间研究如何使用PHP中的API V3使用Google日历执行以下操作 插入新事件 读取所有现有事件 删除每个现有事件 然而,我仍然想知道如何清除整个谷歌日历,使我的代码更快,因为read&delete方法有点慢 我一直在尝试如何使用提供的Google函数“clear”,Google提供的文档简单地表明,我应该能够使用以下命令来实现这一点: $service->calendars->clear('primary'); 谷歌代码中还有一条与“日历”方法集合相关的注释(其中存

我花了很长时间研究如何使用PHP中的API V3使用Google日历执行以下操作

  • 插入新事件
  • 读取所有现有事件
  • 删除每个现有事件
  • 然而,我仍然想知道如何清除整个谷歌日历,使我的代码更快,因为read&delete方法有点慢

    我一直在尝试如何使用提供的Google函数“clear”,Google提供的文档简单地表明,我应该能够使用以下命令来实现这一点:

    $service->calendars->clear('primary');
    
    谷歌代码中还有一条与“日历”方法集合相关的注释(其中存在清除功能):

    典型用法是:

    我把它和前面的认证码放在一起。我确信身份验证工作正常,因为我在其他地方使用过,但当我收到错误消息时,清除代码显然是错误的:

    注意:未定义的变量:第39行C:\wamp\www\googleapi\clear\index.php中的服务

    我尝试过使用“primary”和主所有者,我尝试过将日历设置为私有和公共,但没有效果

    任何有明确工作方法的人,请给我指出正确的方向

    这是我目前正在运行的代码:

    <?php
    session_start();
    require_once '../google-api-php-client-master/autoload.php';
    //Google credentials
    $client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
    $service_account_name = 'xxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
    $key_file_location = '../google-api-php-client-master/API Project-xxxxxxx.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 ------------------- */
    $calendarService = new Google_Service_Calendar($client);
    $calendars = $calendarService->calendars;
    $service->calendars->clear('primary');
    ?>
    

    只需使用您的服务日历实例即可

    
    $service=新谷歌服务日历($client);
    $calendar=$service->calendars->clear('primary');
    

    谢谢您的建议,但这会给我错误信息:“string(98)”错误呼叫POST:(403)probled“如果您对代码有任何其他建议或修改,我将不胜感激。
    <?php
    session_start();
    require_once '../google-api-php-client-master/autoload.php';
    //Google credentials
    $client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
    $service_account_name = 'xxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
    $key_file_location = '../google-api-php-client-master/API Project-xxxxxxx.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 ------------------- */
    $calendarService = new Google_Service_Calendar($client);
    $calendars = $calendarService->calendars;
    $service->calendars->clear('primary');
    ?>