【发布时间】:2018-03-27 07:22:09
【问题描述】:
运行此脚本时出现错误。我所有的身份证等都设置好了。代码也到了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() #4 { main} 在第 108 行的 /Applications/XAMPP/xamppfiles/htdocs/mva/vendor/google/apiclient/src/Google/Service/Resource.php 中抛出
这是我的代码。你们知道我做错了什么吗?如果你有问题,就问他们。谢谢!
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 告诉我的那样做。
【问题讨论】:
标签: php google-api google-api-php-client google-calendar-api