0

I'm using Google API PHP client to add events to my calendar. ID is returned successfully after the insert & When I fetch that event using the ID it displays summary & everything correctly & for some reason , the event is showing up in my calendar

I'm using Service Account Credentials. Is there something to be enabled to get this working?

Here's my Code

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>';  
user3339988
  • 404
  • 2
  • 5
  • 12

1 Answers1

1

With the new Google_Client API, you need to share your calendar to yourself.

$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);

Let me know if you still have issue.

SGC
  • 1,025
  • 1
  • 6
  • 6