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

时间:2014-12-02 14:01:03

标签: php google-calendar-api

我使用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>';  

1 个答案:

答案 0 :(得分:1)

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

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