gpt4 book ai didi

push-notification - 使用 PHP 客户端为 Google Calendar API 设置推送通知

转载 作者:行者123 更新时间:2023-12-04 20:44:53 24 4
gpt4 key购买 nike

我想设置 push notifications for Google Calendar API ,每当 Google 日历 api 上的特定资源发生更改时,我的服务器都会收到通知。我想使用 Google APIs client library for PHP 来做到这一点。

但似乎 they don't have a method 用于在 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.
}

我可以在 PHP 中使用 curl 轻松做到这一点,但我的问题是该请求未获得 Google OAuth token 的授权,因此会导致错误。

我想知道是否有解决此问题的方法....

更新

我试图在没有添加正确 header 的情况下将连接发送到 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);

最佳答案

这是我对问题的解决方案。我通过在 Google playground 中摆弄了一下,才知道我做错了什么。 .

我所做的是

  • 我对 POST 请求的正文进行了 JSON 编码,而不是发送 URL 查询参数。
  • 更改了 Authorization POST 请求的一部分 from OAUth {access_token} to Bearer {access_token} .

  • 基本上就是跟着 Google's documentation for setting up push notifications .
    $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);

    关于push-notification - 使用 PHP 客户端为 Google Calendar API 设置推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19648611/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com