Error calling POST (404 Not Found) Google Calendar API - php

I am trying to insert an event with Google Calendar API and PHP. Everytime I submit my form I get the below error message:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/{{MY-CALENDAR-ID-IS-HERE}}#group.calendar.google.com/events: (404) Not Found' in /Applications/MAMP/htdocs/dl-home/classes/google-api/io/Google_REST.php:66 Stack trace: #0 /Applications/MAMP/htdocs/dl-home/classes/google-api/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /Applications/MAMP/htdocs/dl-home/classes/google-api/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /Applications/MAMP/htdocs/dl-home/classes/google-api/contrib/Google_CalendarService.php(469): Google_ServiceResource->__call('insert', Array) #3 /Applications/MAMP/htdocs/dl-home/includes/add-memo.php(66): Google_EventsServiceResource->insert('56c1bc787c21b50...', Object(Google_Event)) #4 {main} thrown in /Applications/MAMP/htdocs/dl-home/classes/google-api/io/Google_REST.php on line 66
But I dont understand what the problem is, I have my Client ID, Calendar ID and Service Email in my script, I also made sure that the sharing permissions of my calendar are shared to my service email but still no luck. I did find similar issues here and here but those havent helped. I also have review the php sample library prior to asking :)
Here is my script:
session_start();
//Call-in the google client library
require_once '../classes/google-api/Google_Client.php';
require_once '../classes/google-api/contrib/Google_CalendarService.php';
//Service and Client Acc Details
define('CLIENT_ID', '890699386256-mdpm4jr22iu6p6j4mi0bvlpgq602ri7g.apps.googleusercontent.com');
define('SERVICE_ACCOUNT_NAME', '890699386256-mdpm4jr22iu6p6j4mi0bvlpgq602ri7g#developer.gserviceaccount.com');
define('KEY_FILE', '../components/DL-Calendarapi-0a09ec997f6a.p12');
//New Session Open / Set Access Token
$client = new Google_Client();
$client->setApplicationName("Anything-here");
$client->setUseObjects(true);
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
//Set Key Cred Details
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
'https://www.google.com/calendar/feeds/56c1bc787c21b501a73c1e7e5776560f/private/full/',
$key)
);
//Format the date to be 'friendly' with the google event input format
//This takes dates as dd/mm/yyyy and converts to yyyy-mm-dd
$df = DateTime::createFromFormat("d/m/Y", $_GET['dateFrom']);
$dt = DateTime::createFromFormat("d/m/Y", $_GET['dateTo']);
$datet = new DateTime($dt->format("Y-m-d"));
//Custom modify snippet to fix googles timezone error
$datet->modify('+1 day');
$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);
//Create the event (Adding Details)
$event = new Google_Event();
//Event title set..
$event->setSummary($_GET['name']);
$event->setLocation('Anything-here-or-pass-variable');
$event->setDescription($_GET['comment']);
$start = new Google_EventDateTime();
$start->setDate($df->format("Y-m-d"));
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDate($datet->format("Y-m-d"));
$event->setEnd($end);
//Add the Event object to Calendar
$cal->events->insert('56c1bc787c21b501a73c1e7e5776560f#group.calendar.google.com', $event);
//Show success message
echo '<div id="message"><div id="message-body"><p>Event Created!</p></div></div>';

The scope seems wrong: 'https://www.google.com/calendar/feeds/56c1bc787c21b501a73c1e7e5776560f/private/full/'
For API v3 you need to use 'https://www.googleapis.com/auth/calendar'

Related

google-api-php-client connect with key.p12

I'm able to add event to my google calendar with an API keys and OAuth 2.0 client IDs but I want to do this without the authorization screen of google.
I followed the information found on this post:
stackoverflow.com/questions/8995451/how-do-i-connect-to-the-google-calendar-api-without-the-oauth-authentication
For this, I created a 'Service account keys'. I went to my project credentials and select 'Create Credentials > Service Account Key'
Then:
service account > my_project_name
Key type > p12
I saved the key file key.p12
Here is my code:
<?php
// display error, debbug
ini_set('display_errors',1);
session_start();
require_once './google-api-php-client/src/Google_Client.php';
require_once './google-api-php-client/src/contrib/Google_CalendarService.php';
// following values are taken from : console.developers.google.com/apis/credentials?project=projectname credentials
// OAuth 2.0 client IDs : Client ID
const CLIENT_ID = 'xxxx';
// Service account keys : Service account
const SERVICE_ACCOUNT_NAME = 'my_project_name';
// key
const KEY_FILE = './key.p12';
$client = new Google_Client();
$client->setApplicationName("Google Calendar API Quickstart");
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
// Load the key in PKCS 12 format
$key = file_get_contents(KEY_FILE);
$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);
//Save token in session
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
// my code here
$event = new Google_Event(...
$calendarId = 'mycalendarID#group.calendar.google.com';
$event = $cal->events->insert($calendarId, $event);
?>
And here is the error message:
Fatal error: Uncaught Google_AuthException: Error refreshing the OAuth2 token, message: '{
'error' : 'invalid_client',
'error_description' : 'The OAuth client was not found.'
}' in /home/www/google-api-php-client/src/auth/Google_OAuth2.php:288
Stack trace:
#0 /home/www/google-api-php-client/src/auth/Google_OAuth2.php(264): Google_OAuth2->refreshTokenRequest(Array)
#1 /home/www/google-api-php-client/src/auth/Google_OAuth2.php(218): Google_OAuth2->refreshTokenWithAssertion()
#2 /home/www/google-api-php-client/src/service/Google_ServiceResource.php(167): Google_OAuth2->sign(Object(Google_HttpRequest))
#3 /home/www/google-api-php-client/src/contrib/Google_CalendarService.php(469): Google_ServiceResource->__call('insert', Array)
#4 /home/www/goog in /home/www/google-api-php-client/src/auth/Google_OAuth2.php on line 288
It seems that the $client->getAccessToken() isn't set but I don't know why.
Thanks in advance for your help.
Create service account key and download the *.json file that contains private keys. Put the *.json file you just downloaded in a directory of your choosing ( credentials.json file in the example ). Then go to Google Calendar->Share and add service account id on the list.
putenv( 'GOOGLE_APPLICATION_CREDENTIALS=credentials.json' );
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope( 'https://www.googleapis.com/auth/calendar' );
$client->setHttpClient( new GuzzleHttp\Client( [ 'verify' => false ] ) ); // disable ssl if necessary
$service = new Google_Service_Calendar( $client );
// list events
$service->events->listEvents( $calendar_id, array( 'timeMin' => ( new DateTime )->format( DateTime::RFC3339 ) ) );
// add event
$event = new Google_Service_Calendar_Event( $data );
$event = $service->events->insert( $calendar_id, $event );
You can find the Google APIs PHP Client Library package with autoloader in the releases page on GitHub, so the only require_once call necessary is for autoload file:
require_once '/path/to/google-api-php-client/vendor/autoload.php';
And, Google docs may help you further, this is just one way that works for me so it may be useful to you as well.

Google calender update event not working using php and returning Error calling PUT 403 Forbidden

I'm struggling to update an event on google calendar. I'm using google-api-php-client. I want to update description of one event at a time using following code.
my#gmail.com is my primary account.
$currEvent = $service->events->get("my#gmail.com", $event_id);
$currEvent->setDescription("Test DESCRIPTION");
$service->events->update("my#gmail.com", $currEvent->getId(), $currEvent);
I'm using above code to update.
Get event is returning proper result but update event is throwing following error:
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling PUT https://www.googleapis.com/calendar/v3/calendars/my%40gmail.com/events/d6ao1oaiaa7s0aif229btheiv4?key=AIzaSyBQJ2hQzpYn8UIL97VKkg3tBFTq9nFAQUE: (403) Forbidden' in /google-api-php-client/src/Google/Http/REST.php:76 Stack trace: #0 /google-api-php-client/src/Google/Http/REST.php(41): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) #1 /google-api-php-client/src/Google/Client.php(548): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 /google-api-php-client/src/Google/Service/Resource.php(190): Google_Client->execute(Object(Google_Http_Request)) #3 / in /google-api-php-client/src/Google/Http/REST.php on line 76
I've tried adding an event to google calendar using following code and it's working:
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('test location');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-12-12T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-12-12T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('abc#test.com');
// ...
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
Any help is most appreciated.
Cheers!!!

Google API / Calendar : can't find a way to use google api

I'm trying to use Google calendar api and its php client library to make a php function that add an event with the variables i give it through parameters.
But it seems that the documentation is outdated, and i can't find a good tutorial to help me. Here is what I've done for the moment :
<?php
require_once 'google-api-php-client/autoload.php';
session_start();
//require_once 'google-api-php-client/src/Google/Client.php';
//require_once 'google-api-php-client/src/Google/Service/Calendar.php';
$client = new Google_Client();
$client->setApplicationName("test");
$client->setClientId("xxxx.apps.googleusercontent.com");
$client->setClientSecret("xxxx");
$client->setRedirectUri("http://localhost/");
$client->setDeveloperKey("xxxx");
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-10-16T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-10-16T10:25:00.000-07:00');
$event->setEnd($end);
/*$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;*/
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
?>
And my browser tells me :
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=xxxx: (401) Login Required' in C:\wamp\www\cnsi\google-api-php-client\src\Google\Http\REST.php on line 76
Could someone help me please ?
I recommend that you used libraries of Zend Framework for it.
checkout http://framework.zend.com/manual/1.12/en/zend.gdata.calendar.html

No events being shown when retrieving events using google calendar

I am encountering a problem with my code when I am trying to retrieve events on the google calendar. Below is my code:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('client id');
$client->setClientSecret('client secret');
$client->setRedirectUri('redirect uri');
$client->setDeveloperKey('developer key');
$cal = new Google_CalendarService($client);
$events = $cal->events->listEvents('email address for google calendar');
echo $events;
?>
When I print out the events of my google calendar, all I get is an empty array. However I am sure that I have several events in my google calendar.
In fact, I event tried to add an event by using the code below:
$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2013-08-23T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-08-23T10:25:00.000-07:00');
$event->setEnd($end);
echo $events;
$createdEvent = $cal->events->insert('email address for google calendar', $event);
However, when I do this code I end up receiving an error that says:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/'email address for google calendar'/events?key='key'......................................lib/google-api-php-client/src/io/Google_REST.php on line 66
Can someone help me on this?
As it's an array you need to use print_r instead of echo. Try replacing:
echo $events;
with
print"<pre>".print_r($events, true)."</pre>";

Google Calendar Events API throwing error

I have my application authenticated using OAuth - and I've tested it with the calendar API. Everything works. I also used the "quick add" method for the events API, and that worked fine. But when I tried to insert using the following code
$event = new Event();
$event->setSummary('hello there');
$event->setLocation('America/Los_Angeles');
$start = new EventDateTime();
$start->setDateTime('2012-04-19');
$event->setStart($start);
$createdEvent = $cal->events->insert('myemail#gmail.com', $event);
I get the following error:
Fatal error: Uncaught exception 'apiServiceException' with message
'Error calling POST
https://www.googleapis.com/calendar/v3/calendars/myemail#gmail.com/events?key=AIzaSyBddYIgnZJrXHuT8gvX-0tEypxsycw99rA:
(400) Bad Request' in
C:\xampp\htdocs\uis\google-api-php-client\src\io\apiREST.php:86 Stack
trace: #0
C:\xampp\htdocs\uis\google-api-php-client\src\io\apiREST.php(56):
apiREST::decodeHttpResponse(Object(apiHttpRequest)) #1
C:\xampp\htdocs\uis\google-api-php-client\src\service\apiServiceResource.php(187):
apiREST::execute(Object(apiServiceRequest)) #2
C:\xampp\htdocs\uis\google-api-php-client\src\contrib\apiCalendarService.php(493):
apiServiceResource->__call('insert', Array) #3
C:\xampp\htdocs\uis\google-api-php-client\examples\calendar\cal.php(44):
EventsServiceResource->insert('paine1591#gmail...', Object(Event)) #4
{main} thrown in
C:\xampp\htdocs\uis\google-api-php-client\src\io\apiREST.php on line
86
with myemail#gmail.com being the ID of my calendar. It works for everything else.
What am I doing wrong?
It appears that you're not setting the attendees of the event.
Example:
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$event->end = $end;
$attendee1 = new EventAttendee();
$attendee1->email = 'attendeeEmail';
$attendees = array($attendee1, ...);
$event->attendees = $attendees;

Categories