创建新事件时,Google日历API PHP中的错误



我在运行此脚本时出现错误。我所有的身份证都设定了。该代码还达到了if($client->getAccessToken())。我想将一个事件插入我的日历中,但这并不能如预期的那样工作。我得到这个错误:

致命错误:未定义的错误:呼叫未定义的方法Google_service_calendar_event :: tosimpleobject((:#0/applications/xampp/xamppfiles/htdocs/mva/vendor/google/google/apiclient-services/src/google/service/service/calendar/calendar/calendar/calendar/resource/events.php (126(:google_service_resource->'google_service _...'(#1/applications/xampp/xamppfiles/htdocs/mva/controllers/google.php(56(:google_service_calendar_resource_events_events->xamppfiles/htdocs/mva/libs/bootstrap.php(53(:google-> index((#3/applications/xampp/xampp/xamppfiles/htdocs/mva/index.php (17(主}投入/applications/xampp/xamppfiles/htdocs/mva/vendor/google/google/apiclient/src/google/service/service/service/resource.php on 108

这是我的代码。你们知道我做错了什么吗?如果您有疑问,请问他们。谢谢!

Session::init();    
include('GoogleClient/Collection.php');
include('GoogleClient/Exception.php');
include('GoogleClient/Client.php');
include('GoogleClient/Service.php');
include('GoogleClient/autoload.php');
$client = new Google_Client();
$client->setApplicationName('doesntmatter-whateveryouwant');
$client->setClientId('xxxxxxxx');
$client->setClientSecret('xxxxxx');
$client->setRedirectUri('http://localhost/mva/index');
$client->setDeveloperKey('xxxxxxxxx');
$cal = new Google_Service_Calendar($client);
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()) {
  echo "<hr><font size=+1>I have access to your calendar</font>";
  $event = new Google_Service_Calendar_Event();
  $event->setSummary('Halloween');
  $event->setLocation('The Neighbourhood');
  $start = new Google_Service_Calendar_EventDateTime();
  $start->setDateTime('2018-03-29T10:00:00.000-05:00');
  $event->setStart($start);
  $end = new Google_Service_Calendar_EventDateTime();
  $end->setDateTime('2018-03-29T10:25:00.000-05:00');
  $event->setEnd($end);
  $calendarId = 'primary';
  $events = $cal->events->insert('primary', $event);
$_SESSION['token'] = $client->getAccessToken();
    } else {
      $authUrl = $client->createAuthUrl();
      print "<a class='login' href='$authUrl'>Connect Me!</a>";
    }
            $this->view->render('google/index');   
}

编辑我知道我的错误在此行中出现:

$events = $cal->events->insert('primary', $event);

,但是我正按照文档告诉我的方式这样做。

我很好奇为什么您在插入呼叫后要设置访问令牌,您是否不想这样做?以下示例直接来自文档。

events.insert

$event = new Google_Service_Calendar_Event(array(
  'summary' => 'Google I/O 2015',
  'location' => '800 Howard St., San Francisco, CA 94103',
  'description' => 'A chance to hear more about Google's developer products.',
  'start' => array(
    'dateTime' => '2015-05-28T09:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'end' => array(
    'dateTime' => '2015-05-28T17:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2'
  ),
  'attendees' => array(
    array('email' => 'lpage@example.com'),
    array('email' => 'sbrin@example.com'),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %sn', $event->htmlLink);

最新更新