2014-01-10 66 views
1

我使用這個代碼將事件添加到我的日曆活動添加到谷歌日曆

$client = new Google_Client(); 
$client->setApplicationName("Calendar"); 
$scopes = array('https://www.googleapis.com/auth/prediction', 'https://www.googleapis.com/auth/calendar'); 

$auth_credentials = new Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_NAME, $scopes, $privateKey); 

$client->setAssertionCredentials($auth_credentials); 
$client->setClientId(CLIENT_ID); 

$cal = new Google_Service_Calendar($client); 

try { 
$event = new Google_Service_Calendar_Event(); 
$event->setSummary('Halloween'); 
$event->setLocation('The Neighbourhood'); 
$start = new Google_Service_Calendar_EventDateTime(); 
$start->setDateTime('2014-01-09T10:00:00.000-05:00'); 
$event->setStart($start); 
$end = new Google_Service_Calendar_EventDateTime(); 
$end->setDateTime('2014-01-10T10:25:00.000-05:00'); 
$event->setEnd($end); 

$createdEvent = $cal->events->insert('primary', $event); 
echo $createdEvent->getId()."\n\n"; 
    } 

catch (Exception $ex) 
{ 
    die($ex->getMessage()); 
} 

我得到的事件ID,它打印出來,但是當我看着自己在瀏覽器中日曆 - 絕對沒有。

我在做什麼錯?

+0

這已經出現一段時間了,我要對一些代碼工作使用這個API的客戶端。你的代碼看起來像是一個很好的例子。那麼你是否最終解決了這個問題呢? – Keverw

+1

我解決了它,但我不記得具體如何:(顯然,錯誤不在代碼中,有些權限或谷歌當前登錄。 – oddtwelve

回答

2

您需要將自己的日曆分享給自己。

這是用於新的Google_Client API。

$scope = new Google_Service_Calendar_AclRuleScope(); 
$scope->setType('user'); 
$scope->setValue('Your Email Goes Here'); 

$rule = new Google_Service_Calendar_AclRule(); 
$rule->setRole('owner'); 
$rule->setScope($scope); 

$result = $service->acl->insert('primary', $rule); 

參考:Google Calendar API v3 - Not Creating Event (Server-to-Server Authentication)