1

I'm using this code to add an Event to my Calendar

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

I get the Event ID, it is printed out, but when I look at my calendar in a browser - there is absolutely nothing.

What am I doing wrong?

oddtwelve
  • 1,027
  • 10
  • 17
  • This has been here for awhile, and I'm about to work on some code for a client using this API. Your code looks like it would be a good example to go by. So did you end up solving this yet? – Keverw Mar 26 '14 at 23:24
  • 1
    I solved it, but I don't remember exactly how :( The error was not in the code, apparently. Something with permissions or google current login. – oddtwelve Mar 27 '14 at 10:49

2 Answers2

2

You need to share your calendar to yourself.

This is for the new 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);

Reference: Google Calendar API v3 - Not Creating Event (Server-to-Server Authentication)

Community
  • 1
  • 1
ryanc
  • 51
  • 6
0

Why add code for sharing your calendar with yourself? The classes Google_Service_Calendar_AclRuleScope and Google_Service_Calendar_AclRule cannot be found or downloaded anywhere.

You just need to add the email address of your service: 45792..........@developer.gserviceaccount.com, which can be found in the Credentials section of your application on https://console.cloud.google.com/, to the owner's calendar sharing settings.

blackgreen
  • 34,072
  • 23
  • 111
  • 129