谷歌日历API同步消息成功收到,但没有通知日历更改PHP


Google Calender API Sync message successfully received but no notifications on calendar change PHP

我成功地创建了一个通知通道,并收到了来自Google Calendar V3 API的第一条消息(同步消息),该消息表明它可以找到我的回调url等。但是,当我更改日历时,它不会给我的回调地址任何通知。

x-Goog-Channel-ID:20fdedbf0-a845-11e3-1515e2-0800200c9a6671111
X-Goog-Channel-Expiration:Tue, 18 Mar 2014 15:48:34 GMT
X-Goog-Resource-State:sync
X-Goog-Message-Number:1
X-Goog-Resource-ID:WSX5G_51Ds8C7ZADf8Yemzhfego
X-Goog-Resource-URI:https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json

//代码来自我的请求

session_start();
require_once(dirname(__FILE__)."/../includes/GoogleAPI/Google_Client.php");
require_once(dirname(__FILE__)."/../includes/GoogleAPI/contrib/Google_CalendarService.php");
$this->client = new Google_Client();
$this->client->setApplicationName("Test Application");
$this->client->setUseObjects(true);
if (isset($_SESSION['gapi_token'])) {
    $this->client->setAccessToken($_SESSION['gapi_token']);
} else {
    $key = file_get_contents(dirname(__FILE__). '/../includes/' . $this->key_file);
    $this->client->setAssertionCredentials( new Google_AssertionCredentials( $this->service_account_name , array( 'https://www.googleapis.com/auth/calendar' ), $key ));
    $_SESSION['gapi_token'] = $this->client->getAccessToken();
} 
$this->client->setClientId($this->client_id);
$cal = new Google_CalendarService($this->client);
$event = new Google_Event();
$event->setSummary('Appointment Test'); 
$event->setLocation('Amsterdam');     
/* Start Date
*/
$start = new Google_EventDateTime();
$start->setDateTime('2014-03-10T19:00:00.000+01:00'); 
$event->setStart($start);
/* Eind date
*/
$end = new Google_EventDateTime();
$end->setDateTime('2014-03-10T22:00:00.000+01:00'); 
$event->setEnd($end);
/* Voeg gasten toe
*/
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('[myemailadress]');
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $cal->events->insert('primary', $event);
echo "<pre>Event ID : '" . $this->client->getAccessToken() . "'</pre>";

//Part that creates the notification channel
$token = $this->client->getAccessToken();
$token_decode = json_decode($token);
$calendar = 'primary';
$url = sprintf("https://www.googleapis.com/calendar/v3/calendars/%s/events/watch", $calendar);
$fields = json_encode(array(
    'id'        => "20fdedbf0-a845-11e3-1515e2-0800200c9a6671111", 
    'type'      => "web_hook",
    'address'   => 'https://www.[mydomain].nl/apiero/calendar.push.notifications.php'
));
/* setup POST headers */
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer ' . $token_decode->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);
$response = json_decode($response);
dump($response);

上面的转储显示它创建了通道,但就像我说的,在此之后,当日历中添加或更改某些内容时,我不会收到任何通知(查看下面的转储)

stdClass Object
(
[kind] => api#channel
[id] => 20fdedbf0-a845-11e3-1515e2-0800200c9a6671111
[resourceId] => WSX5G_51Ds8C7ZADf8Yemzhfego
[resourceUri] => https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json
[expiration] => 1395159757000
)
有人能帮我解决这个问题吗?

让我试试。您需要将watch request中的'primary'替换为您希望收到so通知的实际电子邮件地址:

$calendar = 'youremailhere@google.com';// remove primary string that was here
$url =     sprintf("https://www.googleapis.com/calendar/v3/calendars/%s/events/watch", $calendar);
   $fields = json_encode(array(
   'id'        => "20fdedbf0-a845-11e3-1515e2-0800200c9a6671111", 
   'type'      => "web_hook",
   'address'   => 'https://www.[mydomain].nl/apiero/calendar.push.notifications.php'
));