Google Calendar API V3插入事件方法

时间:2014-12-03 02:26:33

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

我正在尝试使用google calendar api v3在Google日历中插入一个事件。 我创建了add_event视图,其中显示了包含各种字段的表单。 我使用post方法将数据传递给控制器​​到insert_event方法。 我正在接收数据,但它没有插入事件。 我认为这一行有问题:if($ client-> getAccessToken()){

有人解释我错了吗?

welcome.php // CI控制器

    <?php

    if (!defined('BASEPATH'))
      exit('No direct script access allowed');

    class Welcome extends CI_Controller {

      public function index() {
        echo 'Google Calendar API EXA';
        //$this->load->view('welcome_message');
        $this->load->view('add_event');
        $this->load->helper('url');
        error_reporting(E_ALL);
        ini_set('display_errors', 'On');
        //including google calendar api
        set_include_path(get_include_path() . PATH_SEPARATOR . $_ENV['OPENSHIFT_REPO_DIR']);
        include('application/libraries/google-api-php-client-exa/autoload.php');
        include('application/libraries/google-api-php-client-exa/src/Google_Client.php');
        include('application/libraries/google-api-php-client-exa/src/contrib/Google_CalendarService.php');

        session_start();

        if ((isset($_SESSION)) && (!empty($_SESSION))) {
          echo "<br>".'There are cookies'."<br>";
          echo "<pre>";
          print_r($_SESSION);
          echo "</pre>";
        }

        $client = new Google_Client();
        $client->setApplicationName("Test Product");
        $client->setClientId('its secret');
        $client->setClientSecret('its secret');
        $client->setRedirectUri('https://googlecalendar-apiexa.rhcloud.com/');
        $client->setDeveloperKey('its secret'); 
        $client->setUseObjects(true);
        $cal = new Google_CalendarService($client);

        if (isset($_GET['logout'])) {
          echo "<br><br><font size=+2>Logging out</font>";
          unset($_SESSION['token']);
        }

        if (isset($_GET['code'])) {
          echo "<br>Google APi= " . $_GET['code'];
          $client->authenticate($_GET['code']);
          $_SESSION['token'] = $client->getAccessToken();
          header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
          echo "<br>I got the token = " . $_SESSION['token']; // <-- not needed to get here unless location uncommented
        }

        if (isset($_SESSION['token'])) {
          echo "<br>".'Access Granted'."<br>";
          $client->setAccessToken($_SESSION['token']);
        }

        $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        echo "<br><br><font size=+2><a href=$url?logout>Logout</a></font>";
      }

      public function insert_event() {
        $summary = $this->input->post('summary');
        $location = $this->input->post('location');
        $timezone = $this->input->post('timezone');
        $start_time = $this->input->post('start_time');
        $end_time = $this->input->post('end_time');
        echo $timezone;
        echo $client;
        if ($client->getAccessToken()) {
          echo 'here';
          $calList = $cal->calendarList->listCalendarList();
          echo "<pre>";
          echo "<hr><font size=+1>I have access to your calendar</font>";
          $event = new Google_Event();
          $event->setSummary($summary);
          $event->setLocation($location);
          $start = new Google_EventDateTime();
          $start->setTimeZone($timezone);
          $start->setDateTime($start_time);
          $event->setStart($start);
          $end = new Google_EventDateTime();
          $end->setTimeZone($timezone);
          $end->setDateTime($end_time);
          $event->setEnd($end);
          echo "<br>" . 'tests';
          $createdEvent = $cal->events->insert('yuvraj.kumbhar@exateam.com', $event);
          echo 'test5' . "<br>";
          //echo $createdEvent->getId();
          echo "</pre>";
          echo "<br><font size=+1>Event created</font>";
          echo "<hr><br><font size=+1>Already connected</font> (No need to login)";
        } else {
          //Request authorization
          $authUrl = $client->createAuthUrl();
          print "<hr><br><font size=+2><a href='$authUrl'>Connect Me!</a></font>";
          print "Please visit:\n$authUrl\n\n";
          /* print "Please enter the auth code:\n";
            $authCode = trim(fgets(STDIN));
            $accessToken = $client->authenticate($authCode);
            $client->setAccessToken($accessToken); */
        }
      }

    }


    /* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */


//add_event.php // CI view

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<?php  $this->load->helper('url'); ?>
 <?php  ?>
<div class="container-fluid">
    <div class="row-fluid">
    </div>
  <?php //echo base_url();?>
    <form method="post"  
          action="<?php echo base_url();?>index.php/welcome/insert_event" 
          name="add_event" 
          id="add_event" 
          role="form"
          >

        <div class="widget-box">
            <div class="widget-title">  
                <h5>
                    Add Event
                </h5>
            </div>                          
            <div class="control-group">
                <label class="control-label"><span class="required">*</span>Summary</label>
                <div class="controls">
                    <input type="text" id="summary" name="summary" value=" <?php echo (isset($summary)) ? $summary : '' ?>"/>

                </div>
            </div>
            <div class="control-group">
                <label class="control-label"><span class="required">*</span>Location</label>
                <div class="controls">
                    <input type="text" id="location" name="location" value="<?php echo (isset($location)) ? $location : '' ?>"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label"><span class="required">*</span>TimeZone</label>
                <div class="controls">
                    <input type="text" id="timezone" name="timezone" value="<?php echo (isset($timezone)) ? $timezone : '' ?>"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label"><span class="required">*</span>Start Time</label>
                <div class="controls">
                    <input type="text" id="start_time" name="start_time" value="<?php echo (isset($start_time)) ? $start_time : '' ?>"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label"><span class="required">*</span>End Time</label>
                <div class="controls">
                    <input type="text" id="end_time" name="end_time" value="<?php echo (isset($end_time)) ? $end_time : '' ?>"/>
                </div>
            </div>
                <br>        <input type="submit" value="Add Event">
            <!--<button type="button" class="btn btn-primary" name="<?php //echo (isset($submit_name)) ? $submit_name : '' ?>"  onclick="handle_submit('<?php //echo $this->form_name ?>', '<?php //echo site_url($this->page_name . "/" . $submit_name) ?>');" value="<?php echo $submit_value; ?>">

                <?php //echo $submit_value; ?> </button>-->
        </div>
    </form>
</div>      

0 个答案:

没有答案