将事件添加到Google日历

时间:2014-01-10 15:31:03

标签: php calendar google-api google-api-php-client

我正在使用此代码将事件添加到我的日历

$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,它被打印出来,但是当我在浏览器中查看我的日历时 - 绝对没有。

我做错了什么?

1 个答案:

答案 0 :(得分: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)