RentYour Paris
RentYour Paris

Reputation: 5

Google Calendar API Service Accounts - Error 403

I have an issue I can't solve. I am trying to insert an event in a public calendar. I managed to do it but I do not want the user to login so I tried the Service Account method but I can't get it working.

Issue:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/rentyourparis.com_h4s4cm7b8e27of4oigpl4651co%40group.calendar.google.com/events?key=AIzaSyA5xDFe5UjuleS-pssxoXTLOsT0ZA6dur0: (403) Access Not Configured. Please use Google Developers Console to activate the API for your project.' in /home/rentyour/www/google-api-php-client/src/Google/Http/REST.php:79 Stack trace: #0 /home/rentyour/www/google-api-php-client/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) #1 /home/rentyour/www/google-api-php-client/src/Google/Client.php(505): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 /home/rentyour/www/google-api-php-client/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request)) #3 /home/rentyour/www/google-api-php-client/src/Google/Service/Calendar.php(1459): Google_Service_Resource->call('insert', Array, 'Google_Service_...') #4 /h in /home/rentyour/www/google-api-php-client/src/Google/Http/REST.php on line 79

• The calendar API is ON

• I used the Service Accounts credentials

• I removed all referrers

Here is the code :

<?php
session_start();


$path = 'google-api-php-client/src/';  
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';


 define('SERVICE_ACCOUNT_NAME', 'XXXXXX-50dv2bprcmuhn6tuicavg2oqc3c692ua@developer.gserviceaccount.com');

/* THE PATH TO THE SECRET KEY GENERATED WHEN YOU REQUESTED THE
 * SERVICE ACCOUNT. The key's name contains it's public key
 * fingerprint, represented below by the string <longhexstring>
 */
define('KEY_FILE', 'XXXXXXXXXXXXXX.p12');

$client = new Google_Client();
$client->setApplicationName("My TEST Application");

/* Note: make sure to call $client->setUseObjects(true) if you want to see
* objects returned instead of data (this example code uses objects)
 */
/* If you already have a session token, use it. Normally this token is
 * stored in a database and you'd need to retrieve it from there. For
 * this demo we'll simply start a new session each time.
 */
session_start();
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}

/* Load the key in PKCS 12 format - remember: this is the file you had to
* download when you created the Service account on the API console.
*/
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key)
);

if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}

/* ------------------------- We are now properly authenticated ------------------- */

$cal = new Google_Service_Calendar($client);

$event = new Google_Service_Calendar_Event();
$event->setSummary('Dinner at Henks house');
               /* what to do, summary of the appointment */
$event->setLocation('Slochteren');            /* yes, it exists */

/* Now, set the start date/time
*/
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-08-10T19:00:00.000+01:00'); /* Or e.g. 2010-08-26T10:40:00+02:00 */
$event->setStart($start);

/* Now, set the end date/time
*/
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-08-10T22:00:00.000+01:00'); /*  2010-08-26T10:40:00+02:00 */
$event->setEnd($end);

/* For now I just set one attendee, but you can create lists of many if you want to
*/
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('[email protected]');
$attendees = array($attendee1);
$event->attendees = $attendees;

/* CREATE THE EVENT IN THE PROPER CALENDAR
*/
$createdEvent = $cal->events-    >insert('[email protected]', $event);

print "<html><body>";
echo "<pre>I created a calendar entry, it's id is '" . $createdEvent->id . "'</pre>";
print "</body></html>";

 /* Here should be some code to store the token in the database again.
 * Just to reminds us we put some useless code here ;-P
 */
if ($client->getAccessToken()) {
  $_SESSION['token'] = $client->getAccessToken();
 }

?>

I've been searching and testing for hours but nothing works fine :(

Thanks for your help

Upvotes: 0

Views: 2139

Answers (2)

RentYour Paris
RentYour Paris

Reputation: 5

I authorised the Service Accounts client_id in the Admin console of Google Apps and also deleted from the API Console the IPs in Referees tab. It worked for me. Hope it will helps

Upvotes: 0

luc
luc

Reputation: 3782

Even though the calendar is public, that does not mean anyone can write into it. For that you will either need to:

  • Share this public calendar with your service account.
  • Create an event on the service account's calendar and invite the public calendar.
  • Or if the calendar is domain owned you can set up your service account to be a friend of the domain (https://developers.google.com/drive/web/delegation).

Upvotes: 1

Related Questions