dtrnish3637 2019-08-20 06:00
浏览 85

谷歌日历api - 没有事件的时间

Good morning !

I working with the google php library and I am able to authenticate my google account to get all my events with this code:

    <?php
session_start();



/////////// AUTHENTIFICATION
require_once __DIR__.'/google-api-php-client-2.2.3/vendor/autoload.php';    
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/termine/index.php';

$client = new Google_Client();
$client->setAuthConfigFile('client.secrets.json');
$client->addScope("https://www.googleapis.com/auth/calendar.readonly");
$client->setAccessType("offline");
$client->setRedirectUri($redirect_uri);

$token = loadToken();

if (is_array($token) and isset($token["access_token"])) {

    $client->setAccessToken($token);

    if(isset($_GET['revoke'])) {
        $client->revokeToken();
        saveToken("");
        echo('<script>location.reload();</script>');
    }

    // token expired
    if($client->isAccessTokenExpired()) { 
        $refreshToken=loadRefreshToken();

        try {
            $newToken=$client->fetchAccessTokenWithRefreshToken($refreshToken);
        } catch (\Google_Service_Exception $e) { 
            handle_exception($e);
        }   

        saveToken($newToken);
    }
} else { // no token --> authentification


    if (!isset($_GET['code'])) { 

        $client->setRedirectUri($redirect_uri);
        $auth_url = $client->createAuthUrl();
        echo('<script>window.location.replace("'.filter_var($auth_url, FILTER_SANITIZE_URL).'");</script>');

    } else {

        try {

            $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
            saveToken($token);  
            $client->setRedirectUri($redirect_uri);
            echo('<script>window.location.replace("'.filter_var($redirect_uri, FILTER_SANITIZE_URL).'");</script>');

        } catch (\Google_Service_Exception $e) { 
            handle_exception($e);
        }   
    }
}

// handle google service exception 
function handle_exception($e) {
    global $client;
    $client->revokeToken();
    saveToken("");
    echo('<script>location.reload();</script>');
}

function loadToken() {
    return unserialize(file_get_contents("token.txt"));
}   

function loadRefreshToken() {
    return unserialize(file_get_contents("token_refresh.txt"));
}   

function saveToken($token) {
    file_put_contents("token.txt",serialize($token));
    if (isset($token['refresh_token'])) file_put_contents("token_refresh.txt", serialize($token));
}   

$service = new Google_Service_Calendar($client);

/////////// AUTHENTIFICATION END



/////////// CHOOSE CALENDAR
if (isset($_GET['newcal'])) unset($_SESSION['calid']);
if (isset($_GET['calid'])) { $_SESSION['calid']=$_GET['calid']; $calendarId=$_GET['calid']; }

if (!isset($_SESSION['calid'])) {
    echo('<h3>Choose a calendar:</h3>');
    $optParams = array( 'maxResults' => 30, 'showDeleted' => false );
    $calendarList = $service->calendarList->listCalendarList($optParams);

    foreach ($calendarList->getItems() as $calendarListEntry) {
        if ($calendarListEntry->getAccessRole() == "owner") {
            echo ("<br><a href='?calid=".$calendarListEntry->getID()."'>".$calendarListEntry->getSummary()."</a>"); 
        }
    }
} else {
    echo ("<br><a href='?newcal'>choose different calendar</a>");   
    $calendarId = $_SESSION['calid'];
}
/////////// CHOOSE CALENDAR END



/////////// SHOW EVENTS
if (isset($calendarId)) {


    $today = new DateTime("today");
    $optParams = array( 'maxResults' => 300, 'timeMin' => $today->format("c"));

   try {
      $results = $service->events->listEvents($calendarId, $optParams);
   } catch (\Google_Service_Exception $e) { 
      handle_exception($e);
   }

   if (count($results->getItems()) > 0) {   
      echo('<table cellpadding="5" cellspacing="5" border="1"><tr><th>Date</th><th>Time</th><th>Duration(minutes)</th><th>Description</th></tr>');  

      foreach ($results->getItems() as $event) {

        $start= new DateTime($event->start->dateTime);
        $end= new DateTime($event->end->dateTime);
        $duration= date_diff($end, $start);
        $minutes=$duration->h*60+$duration->i;
        $summary = $event->getSummary();

        echo('<tr><td>'.$start->format("j.m.Y").'</td><td>'.$start->format("G:i").'</td><td>'.$minutes.'</td><td>'.$event->getSummary().'</td></tr>');
      } 

        echo("</table>");
    }
}
/////////// SHOW EVENTS END
?>
  1. This code shows me all events. But I would like to see, which time is free - without events (for event booking).

  2. Is this best practices to realize this situation or can I do it better? If yes, how? :) I hope that you guys can help me :)

Thanks a lot !

Information: This is the php client library which I used: https://github.com/googleapis/google-api-php-client

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元