How can I insert data (event) into Google Calendar with PHP? -
i trying insert events google calendar, errors everytime. i've been searching hours answer, found nothing problem. tested lot of examples, without success. i'm new in google calendar, , can't understand google api documentation.
i have fullcalendar uses google calendar (gcal) backend. can read data gcal without problems. when try displayed using fullcalendar, when want write test datas gcal gives me errors. want make calendar application serves reservation app. have calendar, displayed enter page, , can reserve appointments, stored in google calendar.
the main problem that, don't know how make connection google calendar writing data. every sample i've seen, solves problem different way, major of them outdated, of them uses sessions, don't need session because calendar want display fix, sees same.
the code:
require_once 'google/src/google/autoload.php'; require_once "google/src/google/client.php"; require_once "google/src/google/service/calendar.php"; $client_id = 'xxxxxxxxxx-f9ltk86b2klat20k1osmfbgpu4u1vqse.apps.googleusercontent.com'; $client_secret = 'xxxxxxxxxxxxxxxxxxxxxxx'; $key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // server key $email_address = 'xxxxxxxxxxxx-f9ltk86b2klat20k1osmfbgpu4u1vqse@developer.gserviceaccount.com'; $client = new google_client(); $client->setapplicationname("calendar"); $client->setclientid($client_id); $client->setclientsecret($client_secret); $client->setdeveloperkey($key); $client->setscopes(array( 'https://www.googleapis.com/auth/plus.login', )); $cal = new google_service_calendar($client); $event = new google_service_calendar_event(); $event->setsummary('title'); $event->setdescription('title'); $event->setlocation('somewhere'); $start = new google_service_calendar_eventdatetime(); $start->setdatetime('2013-08-17t16:00:00.000-07:00'); $event->setstart($start); $end = new google_service_calendar_eventdatetime(); $end->setdatetime('2013-08-17t17:00:00.000-07:00'); $event->setend($end); $createdevent = $cal->events->insert('<calendar id>', $event);
the exception generates:
[19-jun-2015 09:08:59 europe/berlin] php fatal error: uncaught exception 'google_service_exception' message 'error calling post https://www.googleapis.com/calendar/v3/calendars/hpba8d7p1f6l65ruhbl9qigvks%40group.calendar.google.com/events?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx: (401) login required' in /var/www/xxxxxxxxxxxxxxxxxxxx/calendar/google/src/google/http/rest.php:110
login required
means have not authenticated properly.
by looking @ scope using think might give hint why not working
you passing scope google+ should passing 1 of google calendars scopes if want access google calendar data.
- https://www.googleapis.com/auth/calendar read/write access calendars
- https://www.googleapis.com/auth/calendar.readonly read-only access calendars
authenticate service account
<?php require_once 'google/autoload.php'; session_start(); /************************************************ following 3 values befound in setting application created on google developers console. developers console. key file should placed in location not accessable web. outside of web root. in order access ga account must add email address user @ account level in ga admin. ************************************************/ $client_id = '[your client]'; $email_address = '[your service account email]'; $key_file_location = '[your key]'; $client = new google_client(); $client->setapplicationname("client_library_examples"); $key = file_get_contents($key_file_location); // separate additional scopes comma $scopes ="https://www.googleapis.com/auth/calendar"; $cred = new google_auth_assertioncredentials( $email_address, array($scopes), $key ); $client->setassertioncredentials($cred); if($client->getauth()->isaccesstokenexpired()) { $client->getauth()->refreshtokenwithassertion($cred); } $service = new google_service_calendar($client); ?>
code ripped tutorial php google calendar service account
tip: make sure have given service account access calendar in question. need add service account email calendar other user.
Comments
Post a Comment