谷歌日历的推送通知不起作用

时间:2016-08-13 05:24:59

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

我正在使用php watch commant:

 try {
      $service = new Google_Service_Calendar($this->gapi->client);
      $channel =  new Google_Service_Calendar_Channel($this->gapi->client);
      $uuid = '27baf74c-59****************';
      $channel->setId($uuid);
      $channel->setType('web_hook');
      $channel->setToken($uuid);
      $channel->setAddress('https://*********.com/calendar_webhook');
      $watchEvent = $service->events->watch('primary',$channel);
      print_r($watchEvent);
    } catch (Exception $e) {
        print_r($e->getMessage());
    }

之后,我得到了这样的回复$ watchEvent

Google_Service_Calendar_Channel Object
 (
   [internal_gapi_mappings:protected] => Array
    (
    )

     [address] => 
     [expiration] => 1471667969000
     [id] => 27baf74c-59d*****************
     [kind] => api#channel
     [params] => 
     [payload] => 
     [resourceId] => Ga-R5***************
     [resourceUri] =>        https://www.googleapis.com/calendar/v3/calendars/primary/events?key=AIzaS*********************&alt=json
     [token] => 27baf74c-5****************
     [type] => 
    [modelData:protected] => Array
    (
    )

  [processed:protected] => Array
    (
    )

 )

但是在我的通知网址中,当我的日历中的更改时,我没有收到任何消息。我错过了什么!?在我的响应中,地址参数为空。有什么问题吗我已经在谷歌进行了所有网址验证。请帮帮我

1 个答案:

答案 0 :(得分:0)

您必须实施Watch方法。要为有关特定资源更改的消息设置通知渠道,请向资源的POST方法发送watch请求。

每个通知渠道都与特定用户和特定资源相关联。除非当前用户拥有或有权访问此资源,否则监视请求将无法成功。

请求:

https://www.googleapis.com/apiName/apiVersion/resourcePath/watch

开始关注给定日历上的事件集合的更改:

POST https://www.googleapis.com/calendar/v3/calendars/example.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://example.com/notifications", // Your receiving URL.
  ...
  "token": "target=myApp-myCalendarChannelDest", // (Optional) Your channel token.
  "expiration": 1426325213000 // (Optional) Your requested channel expiration time.
  }
}

响应:

{
  "kind": "api#channel",
  "id": "01234567-89ab-cdef-0123456789ab"", // ID you specified for this channel.
  "resourceId": "o3hgv1538sdjfh", // ID of the watched resource.
  "resourceUri": "https://www.googleapis.com/calendar/v3/calendars/example.com/events", // Version-specific ID of the watched resource.
  "token": "target=myApp-myCalendarChannelDest", // Present only if one was provided.
  "expiration": 1426325213000, // Actual expiration time as Unix timestamp (in ms), if applicable.
}

创建新的通知渠道以观看资源后,Google Calendar API会发送同步消息以指示通知正在开始。这些消息的X-Goog-Resource-State HTTP标头值是同步的。由于网络计时问题,甚至在收到watch方法响应之前就可以收到同步消息。