谷歌日历 API PHP 通知过期值

4

我成功地使用PHP注册了Google日历API更改通知:

$service = new Google_Service_Calendar($client);
$channel =  new Google_Service_Calendar_Channel($service);
$channel->setId(<some random ID>);
$channel->setType('web_hook');
$channel->setAddress('https://www.myserver.net/triggerCode.php');
$timetoExpire = time()+3600000;
$channel->setExpiration($timetoExpire);
$watchEvent = $service->events->watch('<some_calendar_name>', $channel);

除非注释掉$channel->setExpiration这一行,否则此方法无效。

我遇到了一个ttl无效的错误。我尝试将过期时间设置为几秒钟(例如6000),几毫秒(例如600000)和time()加上一些毫秒,并且我总是得到相同的错误:

Invalid ttl value for channel -1402834554'

我该如何设置这个呢?
2个回答

4

终于搞定了。这段PHP代码可以运行:

 $service = new Google_Service_Calendar($client);
 $channel =  new Google_Service_Calendar_Channel($service);
 $channel->setId($uniqueID);
 $channel->setType('web_hook');
 $channel->setAddress('https://mydomain.net/notificationCallBack.php');

 $timetoExpire = time()+3600000;
 $optParams = array('ttl' => $timetoExpire);
 $channel->setParams($optParams);

 $watchEvent = $service->events->watch($calendarID, $channel);

上面的过期时间是距今42天,但实际上设置的过期时间是30天。我猜这可能是上限。

2
根据API文档,看起来watch事件不包括expiration参数。该请求需要:
{
  "id": string,
  "token": string,
  "type": string,
  "address": string,
  "params": {
    "ttl": string
  }
}

响应包含过期时间,但不包括请求本身:

{
  "kind": "api#channel",
  "id": string,
  "resourceId": string,
  "resourceUri": string,
  "token": string,
  "expiration": long
}

也许你指的是ttl,它是请求params的一部分?


顺便提一下:看起来expiration也被定义为通知渠道过期的日期和时间,以毫秒表示的Unix时间戳。


在 https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Calendar.php 的 PHP 代码中,有一个对 Google_Calendar_Service_Channel->setExpiration($expiration) 的引用。 - SchroedingersCat
还有一个setPayload函数,但在API文档中没有任何说明。该PHP API实现仍处于测试版阶段。我相信官方API文档胜过测试版实现。 - Mr. Llama
正如Llama所指出的那样,过期时间只应该设置在响应上,而不是请求上。尝试将params.ttl以秒为单位进行设置。 - luc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接