4

如何将事件插入谷歌日历?

我正在使用本指南: https ://developers.google.com/google-apps/calendar/v3/reference/events/insert

在示例部分,有这个 php 代码:

$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
                   // ...
                  );
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);

echo $createdEvent->getId();

但这给了我一个致命的错误,因为 $service 是未定义的。谁能告诉我如何初始化 $service 并使这些东西工作?

4

5 回答 5

3

在最新版本的 Google API v3 PHP 客户端中,您应该使用

$event = new Google_Event(); 

代替

$event = new Event();

并且还使用

$start = new Google_EventDateTime();

代替

$start = new EventDateTime();

当然,需要的定义是不同的,使用这个:

require_once '../src/Google_Client.php';
require_once '../src/contrib/Google_CalendarService.php';
于 2013-09-18T15:38:29.733 回答
2
$service = new apiCalendarService($apiClient);

有关更多信息,请参阅https://developers.google.com/google-apps/calendar/instantiate(切换到右上角的 PHP 源代码)

于 2013-03-14T13:37:43.700 回答
1

我的脚本运行良好,插入、更改和删除 Google Calenda 事件

<!DOCTYPE html>
<!--
***********  GOOGLE CALENDAR  ****************************
Author: Vanderlei Bailo
E-mail: vanbailo@gmail.com
Script: insert, change and delete Google Calenda event
-->
<html>
    <head>
        <title>GOOGLE CALENDAR - insert, change and delete Google Calenda event</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta charset="UTF-8">
        <style>
            body{
                margin: 0;
                width: 100%;
                font-family: Verdana, Arial;
            }
            #centro{
                width: 780px;
                margin: auto;
            }
            .calendario{
                position: relative;
                width: 800px;
                height: 600px;
                margin-left:-390px;
                left: 50%;
                float: left;
                -webkit-border-radius: 5px;
                -moz-border-radius: 5px;
                border-radius: 5px;
            }
            #datahora{
                width: 250px;
                float: left;
            }
            #cento{
                width: 780px;
                float: left;
            }
            #centro .primo{
                width: 100%;
                background-color: #E3E9FF;
                padding: 10px;
                margin: 50px 0;
                float: left;
                -webkit-border-radius: 5px;
                -moz-border-radius: 5px;
                border-radius: 5px;
            }
            label {
                width: 780px;
                margin: 5px 5px 0;
                float: left;
                padding-top: 10px;
            }

            input{
                margin: 5px;
                float: left;
                padding: 5px 10px;
                -webkit-border-radius: 5px;
                -moz-border-radius: 5px;
                border-radius: 5px;
                border: 1px #CCC solid;
            }
            input[type="text"]{
                width: 750px;
            }
            input[type="date"]{
                width: 125px;
            }
            input[type="time"]{
                width: 70px;
            }
            input[type="submit"]{

            }

            input:focus{
                border: 1px  #cc0000 solid;
                box-shadow: 0 0 5px #cc0000;
            }
            .btn {
                background: #3498db;
                background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
                background-image: -moz-linear-gradient(top, #3498db, #2980b9);
                background-image: -ms-linear-gradient(top, #3498db, #2980b9);
                background-image: -o-linear-gradient(top, #3498db, #2980b9);
                background-image: linear-gradient(to bottom, #3498db, #2980b9);
                -webkit-border-radius: 5;
                -moz-border-radius: 5;
                border-radius: 5px;
                font-family: Arial;
                color: #ffffff;
                font-size: 20px;
                padding: 10px 20px 10px 20px;
                text-decoration: none;
                cursor: pointer;
            }

            .btn:hover {
                background: #3cb0fd;
                background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
                background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
                background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
                background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
                background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
                text-decoration: none;
            }

        </style>
    </head>
    <body>

        <?php
        session_start();
        require 'google-api-php-client-master/src/Google/autoload.php';
        require_once 'google-api-php-client-master/src/Google/Client.php';
        require_once 'google-api-php-client-master/src/Google/Service/Calendar.php';

        $client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'; //change this
        $Email_address = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.gserviceaccount.com'; //change this
        $key_file_location = 'xxxxxxxxxxxxxxxxxxxxxxxx.p12'; //change this
        $client = new Google_Client();
        $client->setApplicationName("Client_Library_Examples");
        $key = file_get_contents($key_file_location);


        $scopes = "https://www.googleapis.com/auth/calendar";
        $cred = new Google_Auth_AssertionCredentials(
                $Email_address, array($scopes), $key
        );
        $client->setAssertionCredentials($cred);
        if ($client->getAuth()->isAccessTokenExpired()) {
            $client->getAuth()->refreshTokenWithAssertion($cred);
        }
        $service = new Google_Service_Calendar($client);

        $calendarList = $service->calendarList->listCalendarList();
        while (true) {
            foreach ($calendarList->getItems() as $calendarListEntry) {
                echo "<a href='Oauth2.php?type=event&id=" . $calendarListEntry->id . " '>" . $calendarListEntry->getSummary() . "</a><br>\n";
            }
            $pageToken = $calendarList->getNextPageToken();
            if ($pageToken) {
                $optParams = array('pageToken' => $pageToken);
                $calendarList = $service->calendarList->listCalendarList($optParams);
            } else {
                break;
            }
        }


        if ($_POST) {
            
            $Summary = $_POST['Summary'];
            $Location = $_POST['Location'];
            $DateStart = $_POST['DateStart'];
            $TimeStart = $_POST['TimeStart'];
            $DateEnd = $_POST['DateEnd'];
            $TimeEnd = $_POST['TimeEnd'];
            $acao = $_POST['acao'];



            if ($acao == 'Inserire') {
                //--------------- trying to insert EVENT --------------- 
                $event = new Google_Service_Calendar_Event();
                $event->setSummary($Summary);
                $event->setLocation($Location);
                $start = new Google_Service_Calendar_EventDateTime();
                $datatimeI = geratime(DataIT2DB($DateStart), $TimeStart);

                $start->setDateTime($datatimeI);
                $event->setStart($start);
                $end = new Google_Service_Calendar_EventDateTime();
                $datatimeF = geratime(DataIT2DB($DateEnd), $TimeEnd);

                $end->setDateTime($datatimeF);
                $event->setEnd($end);
                $attendee1 = new Google_Service_Calendar_EventAttendee();
                $attendee1->setEmail('web@ideart.it');
                $attendees = array($attendee1);
                $event->attendees = $attendees;
                $createdEvent = $service->events->insert('primary', $event);
                $_SESSION['eventID'] = $createdEvent->getId();
            } else if ($acao == 'Cancellare') {
                //--------------- trying to del EVENT --------------- 
                $createdEvent = $service->events->delete('primary', $_SESSION['eventID']);
            } else if ($acao == 'Aggiornare') {
                //--------------- trying to update EVENT --------------- 

                $rule = $service->events->get('primary', $_SESSION['eventID']);


                $event = new Google_Service_Calendar_Event();
                $event->setSummary($Summary);
                $event->setLocation($Location);
                $start = new Google_Service_Calendar_EventDateTime();
                $datatimeI = geratime(DataIT2DB($DateStart), $TimeStart);

                $start->setDateTime($datatimeI);
                $event->setStart($start);
                $end = new Google_Service_Calendar_EventDateTime();
                $datatimeF = geratime(DataIT2DB($DateEnd), $TimeEnd);

                $end->setDateTime($datatimeF);
                $event->setEnd($end);
                $attendee1 = new Google_Service_Calendar_EventAttendee();
                $attendee1->setEmail('xxx@xxxxx.xxx'); //change this
                $attendees = array($attendee1);
                $event->attendees = $attendees;

                $updatedRule = $service->events->update('primary', $rule->getId(), $event);
            }
        }

        function DataIT2DB($datapega) {
            if ($datapega) {
                $data = explode('/', $datapega);
                if (count($data) > 1) {
                    $datacerta = $data[2] . '-' . $data[1] . '-' . $data[0];
                } else {
                    $datacerta = $datapega;
                }
            } else {
                $datacerta = $datapega;
            }
            return $datacerta;
        }

        function geratime($DateStart, $TimeStart) {
            $dataHora = $DateStart . 'T' . $TimeStart . ':00.000+02:00'; //Fuso Rome
            return $dataHora;
        }
        ?>

        <div id="contenut" style="width: 100%; float: left;">
            <div id="centro">
                <div class="primo">
                    <form name="adicionar" method="POST" action="#">
                        ID evento: <?php echo ( isset($_SESSION['eventID']) ? $_SESSION['eventID'] : "" ); ?>
                        <input type="hidden" name="" value="<?php echo ( isset($_SESSION['eventID']) ? $_SESSION['eventID'] : "" ); ?>" />
                        <input type="text" name="Summary" value="<?php echo ( isset($_POST['Summary']) ? $_POST['Summary'] : "" ); ?>" placeholder="Titolo"/>
                        <input type="text" name="Location" value="<?php echo ( isset($_POST['Location']) ? $_POST['Location'] : "" ); ?>" placeholder="Località"/>
                        <div id="datahora">
                            <label>Data e ora di inizio</label>
                            <input type="date" name="DateStart" value="<?php echo ( isset($_POST['DateStart']) ? $_POST['DateStart'] : "" ); ?>" placeholder="GG/MM/AAAA"/>
                            <input type="time" name="TimeStart" value="<?php echo ( isset($_POST['TimeStart']) ? $_POST['TimeStart'] : "" ); ?>" placeholder="10:20"/>
                        </div>
                        <div id="datahora">
                            <label>Data e ora di fine</label>
                            <input type="date" name="DateEnd" value="<?php echo ( isset($_POST['DateEnd']) ? $_POST['DateEnd'] : "" ); ?>" placeholder="GG/MM/AAAA"/>
                            <input type="time" name="TimeEnd" value="<?php echo ( isset($_POST['TimeEnd']) ? $_POST['TimeEnd'] : "" ); ?>" placeholder="10:20" />
                        </div>
                        <div id="cento">
                            <input class="btn" type="submit" value="Inserire" name="acao" />
                            <input class="btn" type="submit" value="Cancellare" name="acao" />
                            <input class="btn" type="submit" value="Aggiornare" name="acao" />
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

于 2015-05-06T15:03:54.017 回答
0

它在页面上进行了描述。代码复制如下。

src/config.php

global $apiConfig;
$apiConfig = array(
  // Site name to show in Google's OAuth authentication screen
  'site_name' => 'www.example.org',

  // OAuth2 Setting, you can get these keys on the API Access tab on
  // the Google APIs Console
  'oauth2_client_id' => 'YOUR_CLIENT_ID',
  'oauth2_client_secret' => 'YOUR_CLIENT_SECRET',
  'oauth2_redirect_uri' => 'YOUR_REDIRECT_URL',

  // The developer key; you get this from the Google APIs Console
  'developer_key' => 'YOUR_DEVELOPER_KEY',

  // Which Authentication, Storage and HTTP IO classes to use.
  'authClass' => 'apiOAuth2',

  // Definition of service specific values like scopes, OAuth token URLs, etc
  'services' => array(
      'calendar' => array('scope' => 'https://www.googleapis.com/auth/calendar'),
  )
);

你的脚本

session_start();

require_once "../src/apiClient.php";
require_once "../src/contrib/apiCalendarService.php";

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);

if (isset($_SESSION['oauth_access_token'])) {
  $apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
  $token = $apiClient->authenticate();
  $_SESSION['oauth_access_token'] = $token;
}
于 2013-03-14T13:40:26.487 回答
-1
full code for calendar:
this session has all 

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
//require_once '../../src/vendor/autoload.php';
session_start();

print_r($_SESSION);

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_oauth2_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  $calList = $cal->calendarList->listCalendarList();
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";


 $_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}



?>
于 2016-09-14T07:15:11.073 回答