6

I want to create new event at google calendar from Service Account. I can access correctly and print a list all my calendars. But when I want to create a new event the response is 403 Forbidden.

My code:

require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';

session_start();

const CLIENT_ID = 'XXXXXXXXXX.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'XXXXXXXXXXXX@developer.gserviceaccount.com';

const KEY_FILE = 'google_src/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-privatekey.p12';

$client = new Google_Client();
$client->setApplicationName("APP_NAME");

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,
  'https://www.google.com/calendar/feeds/MY_CALENDAR_ID/private/full/',
  $key)
);

$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary('Bon dia pel matí!');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2012-08-06T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2012-09-06T10:25:00.000-07:00');
$event->setEnd($end);

$createdEvent = $cal->events->insert(CALENDAR_NAME, $event);

And server response:

(403) Forbidden

Any ideas? Maybe other scope, or allow edit events (I dont know where I can configure this and I have searched it, promise)

Of course, my calendar is a public calendar.

Any help will be welcome :)

Thanks!

eMarine
  • 1,158
  • 1
  • 14
  • 26
  • Maybe it's forbidden for a reason? – Peon Aug 08 '12 at 11:04
  • Heh, maybe, but I don't know if there are some place where i can allow to edit events. At calendar configuration I don't see nothing, and I activate calendar API. I have no problems to list caendars, and events. Only edit and create. But really, I spend many time searching this options, and there are nothing to do this :S Thanks – eMarine Aug 08 '12 at 11:06

1 Answers1

14

I solved it, and the issue was not code, It works fine.

The problem was sharing my calendar. Something that I don't knew.

To allow edit your calendar events from service account, do not have enough with all steps you do in the Google Api Console, you have to give permission from your calendar configuration (Calendar configuration -> Share this calendar) to the email account specified into Google Api Console (email adress).

This can be a problem if it's a business account, then you will have to contact the account administrator.

The code shown here works correctly, so I hope it helps someone, just remember that you must use 475 version of the Google Api Client.

You can find at this link: http://google-api-php-client.googlecode.com/svn/trunk/src/

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
eMarine
  • 1,158
  • 1
  • 14
  • 26
  • This worked for me. I just had our account admin share the calendars with my service account's email address. Thanks! – Dan Fischer Nov 05 '12 at 19:35
  • I was looking for solution for hours! Thanks for answer. – stil Apr 07 '13 at 21:37
  • I'm getting the same error however I'm trying to use the calendar belonging to myself from an application that is using my own user as the owner so and when I go to share this calendar page I see my email is listed with access I need. So I'm not really sure what else needs to be done. I do get a warning when I run my code saying : WARNING: Application name is not set. Call Builder#setApplicationName. but when I run the code after this warning I get 403 error. Any idea what this can be? – Amir Peivandi Sep 30 '13 at 15:02
  • I'm getting error with 403 forbidden. when i shared my calendar it display my email id as owner please how to configure??? – Sadikhasan Mar 08 '14 at 12:37
  • I have created a new Account for the Google Api Developer Console and don't have such a API-EMail-Address. My personal GMail-Address I used for creating the API-Projekt doesn't work. – DiableNoir Jun 23 '14 at 07:25
  • It's no longer allowed to share a google calendar with a service account email. – Carl Walsh Jun 12 '22 at 22:41