Insert an event on Google Resource Calendar using the latest google-php-client-api
Posted
by
user3781583
on Stack Overflow
See other posts from Stack Overflow
or by user3781583
Published on 2014-08-19T22:17:57Z
Indexed on
2014/08/19
22:20 UTC
Read the original article
Hit count: 501
google-calendar
|google-api-php-client
- Created a Project
- Enabled Calendar API
- Created an OAuth2.0 Service Account
- Downloaded the keyfile .p12 and saved it locally (not using a server with a public IP address) Shared my Resource Calendar with the Email address created in the Service Account (with Manage Sharing rights)
- Entered Client ID for the service account and authorized http://www.googleapis.com/auth/calendar
Environment lamp setup on localhost.
<?php
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/Calendar.php';
session_start();
const CLIENT_ID = 'XXXXXX.apps.googleusercontent.com'; //Service CLIENT ID
const SERVICE_ACCOUNT_NAME = '[email protected]';
const KEY_FILE = 'google-api-php-client/src/Google/Reservation Service-XXXXXXX.p12';
$client = new Google_Client();
$client->setApplicationName("Appointment");
if (isset($_SESSION['token']))
{
$client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key));
//Save token in session
if ($client->getAccessToken())
{
$_SESSION['token'] = $client->getAccessToken();
}
$cal = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('This is a Test event');
$event->setLocation('Test Location');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-08-20T10:30:00.000-05:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-08-20T12:30:00.000-05:00');
$event->setEnd($end);
$cal->events->insert('[email protected]', $event);
?>
getting the following error:
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/XXXXXXX%40resource.calendar.google.com/events: (403) Forbidden' in /google-api-php-client/src/Google/Http/REST.php:79 Stack trace: #0 /google-api-php-client/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) #1 /google-api-php-client/src/Google/Client.php(503): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 /google-api-php-client/src/Google/Servic/Resource.php(195): Google_Client->execute(Object(Google_Http_Request)) #3 /google-api-php-client/src/Google/Service/Calendar.php(1459): Google_Service_Resource->call('insert', Array, 'Google_Service_...') #4 /calendar.php(53): Google_S in /google-api-php-client/src/Google/Http/REST.php on line 79
A few people had the same issue, I am sharing the calendar with the service account. Any help will be appreciated.
© Stack Overflow or respective owner