Google Calendar API - 停止观看活动

时间:2014-06-06 10:55:11

标签: php oauth-2.0 google-calendar-api google-api-php-client service-accounts

我正在创建一个与Google日历互动的应用,并希望观看资源,但我也希望能够停止观看它们。

当我尝试取消观看时,我收到错误Error calling POST https://www.googleapis.com/calendar/v3/channels/stop: (403) Incorrect OAuth client

我正在使用服务帐户和OAuth 2.0对自己进行身份验证,并且有其他API调用可以完全找到,所以我知道我已获得授权。我做错了什么?

这些是一些有效的API调用......

public function getEvents($calendarId, $opts = array())
{
    $events = $this->calendarService->events->listEvents($calendarId, $opts);
    return $events;
}

public function getEvent($calendarId, $eventId)
{
    $event = $this->calendarService->events->get($calendarId, $eventId);
    return $event;
}

public function watch($calendarId, $channelId, $listener)
{
    $channel = new Google_Service_Calendar_Channel($this->client);
    $channel->setId($channelId);
    $channel->setType('web_hook');
    $channel->setAddress($listener);

    $watchEvent = $this->calendarService->events->watch($calendarId, $channel, array());
    return $watchEvent;
}

这是停止API调用...

public function stopWatch($channelId, $resourceId)
{
    $channel = new Google_Service_Calendar_Channel($this->client);
    $channel->setId($channelId);
    $channel->setResourceId($resourceId);

    $watchEvent = $this->calendarService->channels->stop($channel);
    return $watchEvent;
}

有关如何使其发挥作用的任何想法?

1 个答案:

答案 0 :(得分:0)

您需要从客户端实例化服务,然后从服务实例化通道。可能有快捷方式,但下面的代码有效。

$service = new Google_Service_Calendar($client);
$channel =  new Google_Service_Calendar_Channel($service);
$channel->setId($channelID);
$channel->setResourceId($ResourceID);

try
{
    $service->channels->stop($channel);
}
catch(Exception $e)
{   
    error_log("Caught exception in killWatch ",0);
}