Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup push notifications for Google Calendar API using PHP client

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/[email protected]/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);
like image 581
vikmalhotra Avatar asked Oct 02 '22 14:10

vikmalhotra


2 Answers

I managed to do it via the php client library :

$channel = new Google_Service_Calendar_Channel($client);
$channel->setId('00000000-0000-0000-0000-000000000001');
$channel->setType('web_hook');
$channel->setAddress('https://www.yourserver.com/handleWatch.php');
$watchEvent = $service->events->watch(yourCalendarId, $channel, array());

BR

like image 77
Wim Avatar answered Oct 24 '22 04:10

Wim


Here's my solution to the problem. I figured out what I was doing wrong by fiddling around a bit in Google playground.

What I did was that

  • I JSON-encoded the body of POST request instead of sending a URL query parameters.
  • Changed the Authorization part of POST request from OAUth {access_token} to Bearer {access_token}.

Basically just followed the Google's documentation for setting up push notifications.

$url = sprintf("https://www.googleapis.com/calendar/v3/calendars/%s/events/watch", $calendar);

/* setup the POST parameters */
$fields = json_encode(array(
    'id'        => "some_unique_key",
    'type'      => "web_hook",
    'address'   => sprintf("http://%s//event_status/update_google_events", $_SERVER['SERVER_NAME'])
    ));

/* setup POST headers */
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer ' . $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);
curl_setopt($channel, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($channel, CURLOPT_TIMEOUT, 3);
$response = curl_exec($channel);
curl_close($channel);

error_log($response);
like image 36
vikmalhotra Avatar answered Oct 24 '22 05:10

vikmalhotra