2014-12-02 40 views
0

我正在使用Google API PHP客户端向我的日历添加事件。 ID被插入&后成功返回当我使用它显示总结&一切正确&因某种原因ID,该事件被显示在我的日历通过Google API添加的事件在日历中未显示PHP客户端

我使用的服务帐户凭据获取该事件。有什么可以启用这个工作吗?

这里是我的代码

session_start(); 
    require dirname(__FILE__).'/google-api-php-client-master/autoload.php'; 

    $client_id = '<client ID>'; 
    $service_account_name = '<Service Account Name>'; 
    $key_file_location = dirname(__FILE__).'/API-Project-96c2a9122085.p12'; 


    if (!strlen($service_account_name) || !strlen($key_file_location)) { 
     echo missingServiceAccountDetailsWarning(); 
    } 


    $client = new Google_Client(); 
    $client->setApplicationName("Client_Library_Examples"); 

    $service = new Google_Service_Calendar($client); 


    if (isset($_SESSION['service_token'])) { 
     $client->setAccessToken($_SESSION['service_token']); 
    } 

    $key = file_get_contents($key_file_location); 

    $cred = new Google_Auth_AssertionCredentials($service_account_name,array('https://www.googleapis.com/auth/calendar'),$key); 

    $client->setAssertionCredentials($cred); 

    if ($client->getAuth()->isAccessTokenExpired()) { 
     $client->getAuth()->refreshTokenWithAssertion($cred); 
    } 

    $_SESSION['service_token'] = $client->getAccessToken(); 

    $event = new Google_Service_Calendar_Event(); 
    $event->setSummary("WHY IS THIS NOT WOKING?"); 
    $start = new Google_Service_Calendar_EventDateTime(); 

    $start->setDateTime(date('Y-m-d\TH:i:s',strtotime('2014-12-24 10:00:00'))); 
    $start->setTimeZone('Australia/Melbourne'); 
    $event->setStart($start); 
    $end = new Google_Service_Calendar_EventDateTime(); 
    $end->setDateTime(date('Y-m-d\TH:i:s',strtotime('2014-12-26 12:00:00'))); 
    $end->setTimeZone('Australia/Melbourne'); 
    $event->setEnd($end); 
    $newEvent = $service->events->insert('primary', $event); 
    $newEvent->getId(); 
    $event = $service->events->get('primary', $newEvent->getId()); 
    echo '<pre>'; print_r($event); echo '</pre>'; 

回答

0

随着新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); 

让我知道你是否仍然有问题。

+0

非常感谢,这个伎俩。 – user3339988 2014-12-03 02:50:26

相关问题