vik*_*tra 5 google-calendar-api push-notification google-api-php-client
我想为Google Calendar API设置推送通知,只要Google日历api上的特定资源发生更改,我就会通知我的服务器.我想使用适用于PHP的Google API客户端库来执行此操作.
但似乎他们没有办法在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.
}
Run Code Online (Sandbox Code Playgroud)
我可以在PHP中使用curl轻松地做到这一点,但我的问题是请求未经Google OAuth令牌授权,因此会导致错误.
我想知道这个问题是否有解决方法....
UPDATE
我试图将连接发送到Google而不添加正确的标头,因此我收到授权错误.纠正了那个部分后,我仍然遇到Invalid Credentials错误.这是我的代码片段的样子......
$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);
Run Code Online (Sandbox Code Playgroud)
这是我对问题的解决方案。我通过在Google Playground中摆弄了一下,发现自己做错了什么。
我所做的是
AuthorizationPOST 请求的部分从更改OAUth {access_token}为Bearer {access_token}。基本上只是按照Google 的文档设置推送通知。
$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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4118 次 |
| 最近记录: |