1

I'm trying to add and send for email an event using Google Calendar Api 3 but Google Api returns 403 Forbidden and my calendar is set to public. My code is:

ini_set('display_errors', 1);
require 'Google/Client.php';
require 'Google/Service/Calendar.php';

session_start();

const CLIENT_ID = '**************.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '**************@developer.gserviceaccount.com';
const KEY_FILE = '**************-privatekey.p12';

$client = new Google_Client();
$client->setApplicationName("Calendar App");
//$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_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME, 'https://www.google.com/calendar/feeds/**************@group.calendar.google.com/private/full/',
$key)
);

$client->setClientId(CLIENT_ID);

$cal = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('App Sum');
$event->setLocation('App Location');

$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-05-19T10:30:00.000-05:00');
$event->setStart($start);

$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-05-20T11:30:00.000-05:00');
$event->setEnd($end);

$attendeel = new Google_Service_Calendar_EventAttendee();
$attendeel->setEmail('**************@gmail.com');
$attendees = array($attendeel);
$event->attendees = $attendees;

$cal->events->insert('**************@group.calendar.google.com', $event);

Take the code from http://amazewebs.yitweb.com/ # scriptv4, there is a similar code Edit Google calendar events from Google service account: 403 where the developer says it's not the code if no calendar settings

Do they know if there is some other configuration or if the code is really the problem?

Community
  • 1
  • 1
sgb004
  • 347
  • 4
  • 14

2 Answers2

1

If you have the access to a domain admin account you can post the calendar update as that account without having to login as an admin. You'd do that by filling out all the parameters for $client:

setAssertionCredentials(new Google_Auth_AssertionCredentials(
     SERVICE_ACCOUNT_NAME,           
     'https://www.google.com/calendar/feeds/**************@group.calendar.google.com/private/full/',
     $key,
     'notasecret',
     'http://oauth.net/grant_type/jwt/1.0/bearer',
     'your admin account email');
Andrew
  • 4,953
  • 15
  • 40
  • 58
bobpuffer
  • 11
  • 3
0

I have been playing with this stuff a bit too. I got the list events working, but then switched to adding events and I got this 401. I found out you have to share the calendar with with the **************@developer.gserviceaccount.com email. You do this by going to the calendar > My Calendars > (name) > Share This Calendar, then under Share with Specific people, add that gserviceaccount email and set the appropriate permissions.

Many thanks to this post and this post.

Community
  • 1
  • 1
nomadic_squirrel
  • 614
  • 7
  • 21