【问题标题】:Error in Google Calendar API PHP when creating new event创建新活动时 Google Calendar API PHP 出错
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 这很奇怪。因为如果我复制并粘贴此代码,我会得到与上面完全相同的错误......有些东西告诉我谷歌再次更新了代码或其他东西。
    • vendor 中的脚本正在尝试加载甚至不在该类中的方法...
    • 尝试使用 composer 重新安装库,听起来好像出了点问题。你应该只做 require_once '/path/to/your-project/vendor/autoload.php'; developers.google.com/calendar/quickstart/php
    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多