如何使用PHP将数据(事件)插入Google日历


How can I insert data (event) into Google Calendar with PHP?

我正在尝试将事件插入Google日历,但每次都收到错误。我一直在寻找几个小时来得到答案,但我的问题一无所获。我测试了很多例子,但没有成功。我是谷歌日历的新手,我真的无法理解谷歌 api 文档。

我有一个使用谷歌日历(Gcal)作为后端的完整日历。我可以毫无问题地从 Gcal 读取数据。 当我尝试使用Fullcalendar显示时,但是当我想将一些测试数据写入Gcal时,它给了我错误。我想制作一个日历应用程序,作为预订应用程序。所以我有一个日历,它会显示给进入页面的每个人,他们可以预订约会,这些约会存储在我的谷歌日历中。

主要是问题是,我真的不知道如何连接到Google日历以写入数据。我看到的每个样本,都以不同的方式解决问题,其中主要已经过时,其中一些使用会话,但我不需要会话,因为我想显示的日历是 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);

它生成的异常:

[2015 年 6 月 19 日 09:08:59 欧洲/柏林] PHP 致命错误:未捕获异常"Google_Service_Exception",消息为"调用 POST 时出错" https://www.googleapis.com/calendar/v3/calendars/hpba8d7p1f6l65ruhbl9qigvks%40group.calendar.google.com/events?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX: (401) 需要登录' 在 /var/www/XXXX

.php XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Login Required表示您未正确进行身份验证。

通过查看您正在使用的范围,我认为这可能会给您一个提示,说明为什么它不起作用

"https://www.googleapis.com/auth/plus.login"

您正在传递Google+的范围,如果您想访问Google日历数据,则应传递Google日历范围之一。

  • https://www.googleapis.com/auth/calendar 对日历的读/写访问权限
  • https://www.googleapis.com/auth/calendar.readonly 对日历的只读访问权限

使用服务帐户进行身份验证

<?php
 require_once 'Google/autoload.php';
   session_start();     
/************************************************   
 The following 3 values an befound in the setting   
 for the application you created on Google      
 Developers console.         Developers console.
 The Key file should be placed in a location     
 that is not accessable from the web. outside of 
 web root.   
 In order to access your GA account you must    
 Add the Email address as a user at the     
 ACCOUNT Level in the GA admin.         
 ************************************************/
$client_id = '[your client]';
    $Email_address = '[your service account email]';     
    $key_file_location = '[Your key]';      
    $client = new Google_Client();      
    $client->setApplicationName("Client_Library_Examples");
    $key = file_get_contents($key_file_location);    
// separate additional scopes with a comma   
$scopes ="https://www.googleapis.com/auth/calendar";    
$cred = new Google_Auth_AssertionCredentials(    
    $Email_address,      
    array($scopes),     
    $key         
    );      
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {        
    $client->getAuth()->refreshTokenWithAssertion($cred);       
}       
$service = new Google_Service_Calendar($client);    
?>

从教程 PHP 谷歌日历服务帐户中翻录的代码

提示:请确保您已向服务帐号授予访问相关日历的权限。 您只需要将服务帐户电子邮件添加到日历中,就像任何其他用户一样。