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

如何使用PHP将数据(事件)插入Google日历?,php,google-calendar-api,google-oauth,google-api-php-client,service-accounts,Php,Google Calendar Api,Google Oauth,Google Api Php Client,Service Accounts,我试图将事件插入谷歌日历,但每次都会出错。我已经找了好几个小时想得到答案,但我找不到解决我问题的答案。我测试了很多例子,但没有成功。我是谷歌日历的新手,我不能真正理解谷歌api文档 我有一个完整的日历,它使用谷歌日历(Gcal)作为后端。我可以毫无问题地从Gcal读取数据。当我尝试使用Fullcalendar显示时,但当我想将一些测试数据写入Gcal时,它会给我带来错误。我想制作一个日历应用程序,用作预订应用程序。所以我有一个日历,它向每个进入页面的人显示,他们可以预约,这些预约存储在我的谷歌日

我试图将事件插入谷歌日历,但每次都会出错。我已经找了好几个小时想得到答案,但我找不到解决我问题的答案。我测试了很多例子,但没有成功。我是谷歌日历的新手,我不能真正理解谷歌api文档

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

主要的问题是,我真的不知道如何连接到谷歌日历来写入数据。我看到的每个示例都以不同的方式解决了问题,其中大部分都过时了,有些使用会话,但我不需要会话,因为我要显示的日历是固定的,每个人都看到相同的

代码:

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);
require_once'Google/src/Google/autoload.php';
需要一次“Google/src/Google/Client.php”;
需要一次“Google/src/Google/Service/Calendar.php”;
$client_id='xxxxxxxxx-f9ltk86b2klat20k1osmfbgpu4u1vqse.apps.googleusercontent.com';
$client_secret='xxxxxxxxxxxxxxxxxxxxxxxx';
$key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';//服务器密钥
$email_address='xxxxxxxxxxx-f9ltk86b2klat20k1osmfbgpu4u1vqse@developer.gserviceaccount.com';
$client=新的Google_客户端();
$client->setApplicationName(“日历”);
$client->setClientId($client\u id);
$client->setClientSecret($client\u secret);
$client->setDeveloperKey($key);
$client->setscope(阵列)(
'https://www.googleapis.com/auth/plus.login', 
));
$cal=新谷歌服务日历($client);
$event=新Google_服务_日历_事件();
$event->setSummary('Title');
$event->setDescription('Title');
$event->setLocation('Somewhere');
$start=新的谷歌\服务\日历\事件日期时间();
$start->setDateTime('2013-08-17T16:00:00.000-07:00');
$event->setStart($start);
$end=新的Google_服务_日历_EventDateTime();
$end->setDateTime('2013-08-17T17:00:00.000-07:00');
$event->setEnd($end);
$createdEvent=$cal->events->insert(“”,$event);
它生成的异常:

[19-Jun-2015 09:08:59 Europe/Berlin]PHP致命错误:未捕获的异常“Google\u Service\u exception”,消息为“错误呼叫POST” : (401)需要“登录” /var/www/xxxxxxxxxxxxxxxxx/calendar/Google/src/Google/Http/REST.php:110


需要登录
表示您未正确验证

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

''

你正在传递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);    

?>

从教程中删除的代码


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

谢谢您的快速回答,我用您提供的信息编辑了我的代码,但它给了我以下例外:[19-Jun-2015 10:10:39 Europe/Berlin]PHP致命错误:未捕获异常“Google_Auth_exception”,消息为“无法解析p12文件”。这是一个.p12文件吗?密码正确吗?OpenSSL错误:错误:0D06B08E:asn1编码例程:asn1_D2I_READ_BIO:not low data'in/var/www/xxxxxxxxxxxxx/calendar/Google/src/Google/Signer/P12.php:55问题是,我将服务器Api密钥复制到$Key变量中,我没有使用任何密钥文件,因为我找不到它。您必须使用密钥文件,这就是访问的方式,只是移除它对你没有帮助。如果你的车没油了,你能不加油就开车吗?转到Google开发者控制台并创建一个密钥。我已经创建了两个密钥,一个用于服务器应用程序,一个用于浏览器应用程序。如前所述,我将服务器Api密钥复制到密钥变量:$Key=“”;我在[link]console.developers.google.com[link]上没有看到任何下载键选项,他们可能会删除它,或者我就是找不到。我一直在搜索谷歌,我如何下载密钥文件,但我认为它已经被禁用,他们只提供Api密钥。我必须创建一个新的ClientID-服务帐户,这就是我如何获得密钥文件的方法。现在它终于起作用了,我忘了将我的服务帐户电子邮件添加到我的日历中。谢谢你的帮助,你太棒了!:)