使用PHP客户端为Google Calendar API设置推送通知

时间:2013-10-29 02:31:07

标签: push-notification google-calendar-api google-api-php-client

我想设置 push notifications for Google Calendar API ,只要Google日历api上的特定资源发生变化,我的服务器就会收到通知。我想使用 Google APIs client library for PHP 来执行此操作。

但在PHP库中观看Google日历资源似乎 they don't have a method 。可能是其他图书馆有watch方法,但我对此不太了解。

基本上要设置特定资源的推送通知,您必须将发布请求发送到这样的URL ...

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.
}

我可以在PHP中使用curl轻松实现,但我的问题是请求未经Google OAuth令牌授权,因此会导致错误。

我想知道这个问题是否有解决办法......

更新

我试图将连接发送到Google而不添加正确的标头,因此我收到授权错误。纠正该部分后,我仍然遇到Invalid Credentials错误的问题。这就是我的代码片段......

    $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);

2 个答案:

答案 0 :(得分:2)

这是我解决问题的方法。我通过摆弄Google playground中的一点来弄清楚我做错了什么。

我做的是那个

基本上只是跟随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);

答案 1 :(得分:1)

我设法通过php客户端库来实现:

$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