I want to setup push notifications for Google Calendar API where my server is notified whenever a particular resource on Google calendar api changes. I want to do this using the Google APIs client library for PHP.
But it seems they don't have a method for watching google calendar resources in PHP library. May be other libraries have a watch
method, but I'm not too sure about that.
Basically to setup push notifications for a particular resource you have to send a post request to a URL like this...
POST https://www.googleapis.com/calendar/v3/calendars/my_calendar@gmail.com/events/watch
Authorization: Bearer auth_token_for_current_user
Content-Type: application/json
{
"id": "01234567-89ab-cdef-0123456789ab", // Your channel ID.
"type": "web_hook",
"address": "https://mydomain.com/notifications" // Your receiving URL.
}
I can do that easily using curl in PHP, but my problem is that request isn't authorized with a Google OAuth token, so it results in an error.
I want to know if there is a workaround to this problem....
UPDATE
I was trying to send the connect to Google without adding proper headers so I was getting an authorization error. Having corrected that part, I am still having trouble with an Invalid Credentials
error. Here's what my snippet looks like...
$url = sprintf("https://www.googleapis.com/calendar/v3/calendars/%s/events/watch", $calendar);
/* setup the POST parameters */
$fields = array(
'id' => "some_unique_key",
'type' => "web_hook",
'address' => sprintf("http://%s//event_status/update_google_events", $_SERVER['SERVER_NAME'])
);
/* convert the POST parameters to URL query */
$fields_string = '';
foreach ($fields as $key => $value) {
$fields_string .= sprintf("%s=%s&", $key, $value);
}
rtrim($fields_string, '&');
/* setup POST headers */
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: OAuth ' . $access_token;
/* send POST request */
$channel = curl_init();
curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);
curl_setopt($channel, CURLOPT_URL, $url);
curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
curl_setopt($channel, CURLOPT_POST, true);
curl_setopt($channel, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($channel, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($channel, CURLOPT_TIMEOUT, 3);
$response = curl_exec($channel);
curl_close($channel);
error_log($response);