创建新活动时,Google Calendar API PHP出错

时间:2018-03-27 07:22:10

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

运行此脚本时出现错误。我的所有身份证都已设定。代码也到了if($client->getAccessToken())。我想在我的日历中插入一个事件,但这不符合预期。我收到这个错误:

  

致命错误:未捕获错误:在/Applications/XAMPP/xamppfiles/htdocs/mva/vendor/google/apiclient/src/Google/Service/Resource.php:108中调用未定义的方法Google_Service_Calendar_Event :: toSimpleObject()堆栈跟踪:#0 /Applications/XAMPP/xamppfiles/htdocs/mva/vendor/google/apiclient-services/src/Google/Service/Calendar/Resource/Events.php(126):Google_Service_Resource-> call('insert',Array ,'Google_Service _...')#1 /Applications/XAMPP/xamppfiles/htdocs/mva/controllers/google.php(56):Google_Service_Calendar_Resource_Events-> insert('primary',Object(Google_Service_Calendar_Event))#2 / Applications / XAMPP / xamppfiles / htdocs / mva / libs / Bootstrap.php(53):Google-> index()#3 /Applications/XAMPP/xamppfiles/htdocs/mva/index.php(17):Bootstrap-> __ construct( )第108行/Applications/XAMPP/xamppfiles/htdocs/mva/vendor/google/apiclient/src/Google/Service/Resource.php中的#4 {main}

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

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);

但我正如docs告诉我的那样做。

1 个答案:

答案 0 :(得分:0)

我很好奇为什么你在进行插入调用之后设置访问令牌而你之前不希望这样做?以下示例直接来自文档。

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: %s\n', $event->htmlLink);