doujing6053 2015-09-05 18:05
浏览 37

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

Like so many others before me, I'm having great difficulty getting full functionality to a PHP script that adds/deletes/Updates Google Calendar events.

My code, from the Daimto web site works using Oauth2 in that it lists the current events in my main calendar.

However when I add some code to add an event, the page is blank and I'm unable to trap an error.

The code is:

 <?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>
";


             // 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
      }
 ?>`

Can anyone help?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度