简体   繁体   English

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

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

I am trying to insert events into Google Calendar, but I get errors everytime. 我试图将事件插入Google日历,但每次都会出错。 I've been searching for hours to get answer, but I found nothing for my problem. 我一直在寻找获取答案的时间,但没有发现任何问题。 I tested a lot of examples, without success. 我测试了很多示例,但没有成功。 I'm new in Google Calendar, and I can't really understand the google api documentation. 我是Google日历的新手,我真的不太了解google api文档。

I have a Fullcalendar which uses the Google Calendar (Gcal) as backend. 我有一个使用Google日历(Gcal)作为后端的Fullcalendar。 I can read data from Gcal without any problems. 我可以毫无问题地从Gcal读取数据。 When I try to displayed using Fullcalendar, but when I want to write some test datas into Gcal it gives me errors. 当我尝试使用Fullcalendar显示时,但是当我要将一些测试数据写入Gcal时,却给了我错误。 I want to make a Calendar application which serves as a reservation app. 我想制作一个日历应用程序作为预订应用程序。 So I have a Calendar, which is displayed to everyone who enter the page, and they can reserve appointments, which are stored in my Google Calendar. 因此,我有一个日历,该日历显示给所有进入页面的人,并且他们可以保留约会,这些约会存储在我的Google日历中。

The main is problem is that, I don't really know how to make the connection to Google Calendar for writing data. 主要的问题是,我真的不知道如何建立与Google日历的连接以写入数据。 Every sample I've seen, solves the problem different way, major of them are outdated, some of them uses Sessions, but I don't need Session because the Calendar I want to display is FIX, everyone sees the same. 我所见过的每个示例都以不同的方式解决问题,其中大多数已经过时,其中一些使用Sessions,但是我不需要Session,因为我要显示的Calendar是FIX,每个人都看到相同的结果。

The code: 编码:

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

The exception it generates: 它生成的异常:

[19-Jun-2015 09:08:59 Europe/Berlin] PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/hpba8d7p1f6l65ruhbl9qigvks%40group.calendar.google.com/events?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX : (401) Login Required' in /var/www/XXXXXXXXXXXXXXXXXXXX/calendar/Google/src/Google/Http/REST.php:110 [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 means that you have not authenticated properly. Login Required意味着您没有正确进行身份验证。

By looking at the scope you are using I think that might give you a hint as to why its not working 通过查看您正在使用的示波器,我认为这可能会提示您为什么它不起作用

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

You are passing the scope for Google+ you should be passing one of the Google calendars scopes if you want to access Google calendar data. 您正在传递Google+的范围,如果要访问Google日历数据,则应该传递Google日历的范围之一。

Authenticate with a service account 使用服务帐户进行身份验证

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

?>

code ripped from tutorial PHP Google Calendar service account 从教程PHP Google日历服务帐户中窃取的代码

Tip : make sure that you have given the service account access to the calendar in question. 提示 :确保您已授予服务帐户访问相关日历的权限。 You just need to add the service account email to the calendar like you would any other user. 您只需要像其他用户一样将服务帐户电子邮件添加到日历。

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

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