1
votes

I'm attempting to use the Google Calendar API version3 (PHP) to manage calendars.

I can easily retrieve information regarding a calendar such as calendar names and calendar lists, but I am unsuccessful in actually 'writing' to a calendar such as adding events or creating a secondary calendar. I always receive the following error in my attempt.

Anybody out there that can help? I have looked everywhere in Google's documentation but can't find any answers.

The error (if trying to post, not retrieve info):

Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars?key=eyfheufheuwehwi: (400) Invalid Value' in /home/incs/google-api-php-client/src/io/apiREST.php:86 Stack trace: #0 /home/incs/google-api-php-client/src/io/apiREST.php(56): apiREST::decodeHttpResponse(Object(apiHttpRequest)) #1 /home/incs/google-api-php-client/src/service/apiServiceResource.php(187): apiREST::execute(Object(apiServiceRequest)) #2 /home/incs/google-api-php-client/src/contrib/apiCalendarService.php(228): apiServiceResource->__call('insert', Array) #3 /home/public_html/gateway/google/index.php(130): CalendarsServiceResource->insert(Object(Calendar)) #4 {main} thrown in /home/incs/google-api-php-client/src/io/apiREST.php on line 86

the code (by the way my access keys are set in the config.php hence the commented $client lines)

require("/home/incs/google-api-php-client/src/apiClient.php");
require("/home/incs/google-api-php-client/src/contrib/apiCalendarService.php");
session_start();

$client = new apiClient();
$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_oauth2_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');
$service = new apiCalendarService($client);
if (isset($_GET['logout'])) 
{
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) 
{
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

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

if ($client->getAccessToken()) 
{

  $calendar = new Calendar();
  $calendar->setSummary('calendarSummary');
  $calendar->setTimeZone('American/Los_Angeles');

  $createdCalendar = $service->calendars->insert($calendar);


  $calList = $service->calendarList->listCalendarList();
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";


  $_SESSION['token'] = $client->getAccessToken();
} 
else 
{
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
1

1 Answers

4
votes

As it is right now, Google has an error in their code sample (which is where I got the code that I pasted here). In that sample the following needs to be corrected for it to work:

$calendar->setTimeZone('American/Los_Angeles'); 

needs to be corrected to

$calendar->setTimeZone('America/Los_Angeles');

And then no error will be thrown.