【问题标题】:Setup push notifications for Google Calendar API using PHP client使用 PHP 客户端为 Google Calendar API 设置推送通知
【发布时间】:2013-11-08 00:58:44
【问题描述】:

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

但似乎 they don't have a method 用于在 PHP 库中观看谷歌日历资源。可能其他库有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 错误的问题。这是我的 sn-p 的样子...

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

【问题讨论】:

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


【解决方案1】:

这是我解决问题的方法。通过在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);

【讨论】:

  • 谢谢您,您是如何收到更改通知的?我正在尝试使用 get_headers() 但每当我更改日历中的任何内容时,我都不会在提供的 URL 上收到通知。我怎么知道我收到了通知?
  • 你可能想使用 apache_request_headers() @kakashihatake2
  • @Chandrew 好的,但 Google 不仅仅回拨该页面
  • 那么您可能需要发出实际的监视请求。我建议记录从您的手表通知 URL 传入的任何 POST 请求。
  • @vikkun 知道如何接收远程 php 脚本的推送通知吗?我应该在脚本中添加什么来正确接收和拦截消息?我试过getallheaders()。但它是空的。
【解决方案2】:

我设法通过 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

【讨论】:

  • 变量 $service 是哪个服务?
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多