通过 Google API PHP 客户端添加的事件未显示在日历中


Events added through Google API PHP client not showing in Calendar

我正在使用Google API PHP客户端将事件添加到我的日历中。插入后成功返回 ID &当我使用 ID 获取该事件时,它会正确显示摘要和所有内容以及出于某种原因,该事件显示在我的日历中

我正在使用服务帐户凭据。有什么东西可以启用它来让它工作吗?

这是我的代码

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>';  

使用新的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);

如果您仍有问题,请告诉我。