如何使用API​​ V3 PHP在Google日历中阅读活动和删除所有活动 [英] How to read events and delete all events in Google Calendar using API V3 PHP

查看:238
本文介绍了如何使用API​​ V3 PHP在Google日历中阅读活动和删除所有活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Google API文档和搜索网页后,我设法编写了一个简单的PHP函数,将事件插入到我的Google日历中(如果有人想要该代码,请问)。



但是,我想做的是删除我的Google日历的整个内容。最初我想我会通过读取所有事件,然后删除每一个,但根据Google我可以在一个单一的命令:



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



但是,由于Google API文档仅涵盖实际的命令,因此没有显示任何需要的代码,因此,我使用在事件插入脚本中工作的授权代码,但我只是不能得到它的工作,以清除数据,我得到错误:



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



我的整个代码如下:

 <?php 
//
//示例代码取自:
// https://developers.google.com/google-apps/calendar/ v3 / reference / calendars / clear
//
//
calendclear('xxxxxxxxxxx@googlemail.com');

// ---------------------------------------- ----------- //
//功能从Google日历中删除所有活动//
// --------------- ------------------------------------ //
function calendclear($ cal_id){
session_start();
require_once'../google-api-php-client-master/autoload.php';
// Google凭证
$ 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(无论应用程序的名称是什么);
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;
//删除所有日历数据
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的引用是日历所有者电子邮件地址 - 删除不会与'primary'或$ service_account_name。



如果你正在玩这个东西,我希望这有帮助,因为那么我生命的最后2天,我试图工作,这不是完全浪费:)

 <?php 
session_start
require_once'../google-api-php-client-master/autoload.php';
// Google凭证
$ 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(无论应用程序的名称是什么);
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();

/ * -------------------------我们现在已正确验证---------- --------- * /

$ cal = new Google_Service_Calendar($ client);
$ event_list = $ cal-> events-> listEvents('xxxxxxxxxxxx@googlemail.com');

$ events = $ event_list-> getItems();

//通过从日历读取的所有事件循环

foreach($ events as $ event)
{
$ myEvent = $ event-> getId();
$ mySummary = $ event-> getSummary();
$ MyStart = $ event-> getStart() - > getDateTime();
$ MyEnd = $ event-> getEnd() - > getDateTime();
echo'即将删除事件'。$ mySummary。'ID:('。$ myEvent。')< br />';
//现在删除找到的事件
try {
$ cal-> events-> delete('xxxxxxxxxxxxxx@googlemail.com',$ myEvent);
echo'Success。< br />';
} catch(Exception $ e){
var_dump($ e-> getMessage());
die('Event Delete Failed。');
}
}
?>


After hours of reading Google API documentation and searching the web I managed to write a simple PHP function to insert an event into my Google Calendar (if anyone wants the code just ask).

However the next thing I want to do is to delete the entire content of my Google calendar. Initially I thought I'd do this by reading all events and then deleting each one, but according to Google I can do this in a single command:

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

However since the Google API documentation only covers the actual command and does not show any of the code that needs to precede this, so I've used the authorisation code that worked in the event insert script, but I just cannot get it to work for clearing the data, and I'm getting error:

Notice: Undefined variable: service in index.php on line 68

My entire code follows:

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

解决方案

After much trial and error I found a way to clear the Google Calendar using the API v3.

I was unable to get the clear command to work, so instead do this I had to: 1. read all events in the calendar 2. delete each one

Firstly ensure your Google Calendar is setup correctly to allow any of this stuff to work - there are 4 simple steps detailed in the answer here Inserting Google Calendar Entries with Service Account

Next, here is the code I used - note the last 2 references to xxxxxxxxxxxx@googlemail.com are the calendar owner email address - the delete would NOT work with either 'primary' or the $service_account_name.

If you are playing with this stuff I hope this helps, as then the last 2 days of my life I spent trying to work this out were not totally wasted :)

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

这篇关于如何使用API​​ V3 PHP在Google日历中阅读活动和删除所有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆