繁体   English   中英

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

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

我试图将事件插入Google日历,但每次都会出错。 我一直在寻找获取答案的时间,但没有发现任何问题。 我测试了很多示例,但没有成功。 我是Google日历的新手,我真的不太了解google api文档。

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

主要的问题是,我真的不知道如何建立与Google日历的连接以写入数据。 我所见过的每个示例都以不同的方式解决问题,其中大多数已经过时,其中一些使用Sessions,但是我不需要Session,因为我要显示的Calendar是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日,欧洲/柏林] 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

Login Required意味着您没有正确进行身份验证。

通过查看您正在使用的示波器,我认为这可能会提示您为什么它不起作用

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

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

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

<?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 Google日历服务帐户中窃取的代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM