I need to add event via php on server for a gmail account. So I need a service account not a web application or installed application. Web application is so you can add events via your server to other peoples calendars. For this we want to add to 1 server calendnar.
This code works great as of 0.6.7 version of the google-api-php-client.
require_once dirname(__FILE__).'/google-api-php-client/src/Google_Client.php';
require_once dirname(__FILE__).'/google-api-php-client/src/contrib/Google_CalendarService.php';
ini_set('display_errors', 1);
define('CLIENT_ID','your-client-id-from-service-account-google-api-console');
define('SERVICE_ACCOUNT_NAME','your-email-address-from-service-account');
define('KEY_FILE',dirname(__FILE__).'/location-of-the-p12-file-your-downloaded-when-creating-service-account.p12');
$client = new Google_Client();
$client->setApplicationName("your-app-name-doesnt-matter-what-you-put-here");
$client->setUseObjects(true); //IF USING SERVICE ACCOUNT (YES)
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key));
$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);
Add new comment