【发布时间】:2015-09-05 02:55:54
【问题描述】:
我正在尝试将活动插入 Google 日历,但每次都会出错。我一直在寻找几个小时来获得答案,但我没有找到任何问题。我测试了很多例子,没有成功。我是 Google 日历的新手,对 google api 文档不太了解。
我有一个使用 Google 日历 (Gcal) 作为后端的 Fullcalendar。我可以毫无问题地从 Gcal 读取数据。当我尝试使用 Fullcalendar 显示时,但是当我想将一些测试数据写入 Gcal 时,它给了我错误。我想制作一个用作预订应用程序的日历应用程序。所以我有一个日历,它显示给每个进入页面的人,他们可以预订约会,这些约会存储在我的谷歌日历中。
主要问题是,我真的不知道如何连接到 Google 日历以写入数据。我看到的每个示例,解决问题的方式都不一样,其中专业已经过时,其中一些使用 Sessions,但我不需要 Session,因为我要显示的日历是 FIX,每个人看到的都一样。
代码:
require_once 'Google/src/Google/autoload.php';
require_once "Google/src/Google/Client.php";
require_once "Google/src/Google/Service/Calendar.php";
$client_id = 'XXXXXXXXXX-f9ltk86b2klat20k1osmfbgpu4u1vqse.apps.googleusercontent.com';
$client_secret = 'XXXXXXXXXXXXXXXXXXXXXXX';
$key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Server Key
$email_address = 'XXXXXXXXXXXX-f9ltk86b2klat20k1osmfbgpu4u1vqse@developer.gserviceaccount.com';
$client = new Google_Client();
$client->setApplicationName("Calendar");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setDeveloperKey($key);
$client->setScopes(array(
'https://www.googleapis.com/auth/plus.login',
));
$cal = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Title');
$event->setDescription('Title');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2013-08-17T16:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2013-08-17T17:00:00.000-07:00');
$event->setEnd($end);
$createdEvent = $cal->events->insert('<Calendar ID>', $event);
它产生的异常:
[19-Jun-2015 09:08:59 Europe/Berlin] PHP 致命错误:未捕获的异常“Google_Service_Exception”和消息“调用 POST 时出错” https://www.googleapis.com/calendar/v3/calendars/hpba8d7p1f6l65ruhbl9qigvks%40group.calendar.google.com/events?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX: (401) 需要登录 /var/www/XXXXXXXXXXXXXXXXXXXX/calendar/Google/src/Google/Http/REST.php:110
【问题讨论】:
标签: php google-calendar-api google-oauth google-api-php-client service-accounts