使用PHP客户端设置Google Calendar API的推送通知 [英] Setup push notifications for Google Calendar API using PHP client

查看:140
本文介绍了使用PHP客户端设置Google Calendar API的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置 针对Google Calendar API的推送通知 ,每当Google Calendar api上的特定资源发生变化时,我的服务器就会收到通知.我想使用 适用于PHP的Google API客户端库来执行此操作 .

I want to setup push notifications for Google Calendar API where my server is notified whenever a particular resource on Google calendar api changes. I want to do this using the Google APIs client library for PHP.

但是似乎 他们没有有一种方法 用于查看PHP库中的Google日历资源.可能其他库也有watch方法,但是对此我不太确定.

But it seems they don't have a method for watching google calendar resources in PHP library. May be other libraries have a watch method, but I'm not too sure about that.

基本上要为特定资源设置推送通知,您必须将发布请求发送到这样的URL ...

Basically to setup push notifications for a particular resource you have to send a post request to a URL like this...

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令牌的授权,因此会导致错误.

I can do that easily using curl in PHP, but my problem is that request isn't authorized with a Google OAuth token, so it results in an error.

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

I want to know if there is a workaround to this problem....

更新

我试图在不添加适当标题的情况下将连接发送给Google,因此出现授权错误.更正了该部分之后,我仍然遇到Invalid Credentials错误的麻烦.这是我的代码段的样子...

I was trying to send the connect to Google without adding proper headers so I was getting an authorization error. Having corrected that part, I am still having trouble with an Invalid Credentials error. Here's what my snippet looks like...

    $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游乐场中稍微摆弄一点,我发现了我在做错什么.

Here's my solution to the problem. I figured out what I was doing wrong by fiddling around a bit in Google playground.

我所做的就是

基本上只是遵循 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);

这篇关于使用PHP客户端设置Google Calendar API的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆