使用PHP将事件添加到Google日历时出错

时间:2015-09-05 18:05:15

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

像我之前的其他许多人一样,我很难获得添加/删除/更新Google日历活动的PHP脚本的完整功能。

我的代码,来自Daimto网站,使用Oauth2,因为它列出了我主日历中的当前事件。

但是,当我添加一些代码来添加事件时,该页面是空白的,我无法捕获错误。

代码是:

 <?php    


    require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');  

    session_start();   

    // ********************************************************  //
    // Get these values from https://console.developers.google.com
    // Be sure to enable the Analytics API
    // ********************************************************    //
    $client_id = 'xxx.apps.googleusercontent.com';
    $client_secret = 'xxxkQO8K';
    $redirect_uri = 'xxx/GoogleCalendar/tests/MGC_NEW.php';

    $client = new Google_Client();
    $client->setApplicationName("primary");
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->setAccessType('offline');   // Gets us our refreshtoken

    $client->setScopes(array('https://www.googleapis.com/auth/calendar'));


    //For loging out.
    if (isset($_GET['logout'])) {
    unset($_SESSION['token']);
     }


    // Step 2: The user accepted your access now you need to exchange it.
     if (isset($_GET['code'])) {

     $client->authenticate($_GET['code']);  
     $_SESSION['token'] = $client->getAccessToken();
     $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }

     // Step 1:  The user has not authenticated we give them a link to login    
     if (!isset($_SESSION['token'])) {

     $authUrl = $client->createAuthUrl();

     print "<a class='login' href='$authUrl'>Connect Me!</a>";
     }    


     // Step 3: We have access we can now create our service
     if (isset($_SESSION['token'])) {
     $client->setAccessToken($_SESSION['token']);
     print "<a class='logout' href='http://www.marchgolfclub.co.uk?logout=1'>LogOut</a><br>";   

     $service = new Google_Service_Calendar($client);    

     $calendarList  = $service->calendarList->listCalendarList();

     //print_r($calendarList);

     while(true) {
          foreach ($calendarList->getItems() as $calendarListEntry) {

             echo $calendarListEntry->getSummary()."<br>\n";


             // get events 
              $events = $service->events->listEvents($calendarListEntry->id);


              foreach ($events->getItems() as $event) {
                 /*Code to add holiday and Bank Holidays etc.*/
                 //echo "-----".$event->getSummary()."<br>";
             }
         }
         $pageToken = $calendarList->getNextPageToken();
         if ($pageToken) {
             $optParams = array('pageToken' => $pageToken);
             $calendarList = $service->calendarList-  >listCalendarList($optParams);
        } else {
            break;
         }
     }

     //THIS IS WHERE THE CODE BREAKS DOWN
    echo "<hr><font size=+1>I have access to your calendar</font>";

    $event = new Google_Service_Calendar_Event($client);
     $event->setSummary(‘Interview’);
    $event->setLocation(‘Hell’);
     $start = new Google_Service_Calendar_EventDateTime();
     $start->setDateTime(‘2015-10-03T10:00:00.000-07:00’);
     $event->setStart($start);
     $end = new Google_Service_Calendar_EventDateTime();
     $end->setDateTime(‘2015-10-03T10:25:00.000-07:00′);
      $event->setEnd($end);
      $attendee1 = new Google_Service_Calendar_EventAttendee();
     $attendee1->setEmail(’email@abc.com’);
 // …
     $attendees = array($attendee1);
     $event->attendees = $attendees;

     //$createdEvent = $service->events->insert(‘primary’, $event);

     //Google code error was fixed
      $createdEvent = $service->events->insert($calName, $event);

     //echo $createdEvent->getId();

      var_dump($createdEvent);

     echo "<br><font size=+1>Event created</font>";

    //END OF BROKEN CODE
      }
 ?>`

有人可以帮忙吗?

0 个答案:

没有答案