簡體   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