创建事件Google日历错误

时间:2018-08-08 02:01:24

标签: php cmd google-calendar-api

我是一名入门程序员,是Google Calendar API的新手。 我想使用Google提供的代码创建一个活动来创建一个活动,问题是我只能获取即将发生的活动的列表,但无法创建活动。

此代码有效:https://developers.google.com/calendar/quickstart/php

我将此代码放在另一个代码的下面:

$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' => '2018-08-28T09:00:00-07:00',
    'timeZone' => 'Europe/Brussels',
  ),
  'end' => array(
    'dateTime' => '2018-08-28T17:00:00-07:00',
    'timeZone' => 'Europe/Brussels',
  ),
  'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2'
  ),
  'attendees' => array(
    array('email' => 'stefanos.stoikos@gmail.com'),
    array('email' => 'stefanos.stoikos@gmail.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);

当我在cmd中执行此操作时,出现以下错误:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}
' in C:\xampp\htdocs\StefanosProject\vendor\google\apiclient\src\Google\Http\REST.php:118
Stack trace:
#0 C:\xampp\htdocs\StefanosProject\vendor\google\apiclient\src\Google\Http\REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 C:\xampp\htdocs\StefanosProject\vendor\google\apiclient\src\Google\Task\Runner.php(176): call_user_func_array(Array, Array)
#3 C:\xampp\htdocs\StefanosProject\vendor\google\apiclient\src\Google\Http\REST.php(58): Google_Task_Runner->run()
#4 C:\xampp\htdocs\StefanosProject\vendor\google\apiclient\src in C:\xampp\htdocs\StefanosProject\vendor\google\apiclient\src\Google\Http\REST.php on line 118

我尝试了不同的解决方案来创建活动,但是我一直出错,有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

您的创建事件代码很可能是正确的,但是您需要为要将事件添加到的用户配置oauth:

https://developers.google.com/calendar/create-events

具体在此处查看此部分:

In order to successfully create events, you need to:

* set your OAuth scope to https://www.googleapis.com/auth/calendar.
ensure the authenticated user has write access to the calendar 
* with the calendarId you provided (for example by calling calendarList.get() for the calendarId and checking the accessRole).

这里有一个更长的教程,其中包括设置和使用oauth: http://usefulangle.com/post/29/google-calendar-api-create-event-php

答案 1 :(得分:0)

这就是正在发生的事情。您引用的PHP快速入门使用read-only范围-CALENDAR_READONLY。但是,您要执行的操作需要写作用域,因为您要进行create events。因此,将您的范围更改为 https://www.googleapis.com/auth/calendar或其他PHP语言版本。

更改后,请不要忘记删除以前保存的凭据,以使新范围生效。