【问题标题】:Receiving Google Drive Push Notifications接收 Google Drive 推送通知
【发布时间】:2017-05-13 04:52:53
【问题描述】:

通过使用Not receiving webhook notification from drive, why? 此处描述的方法,我已经能够建立一个通道来接收来自 Google Drive 的推送通知。我收到通知,一切正常。我的问题是,当我收到推送通知时,我只收到以下信息:

Content-Length: 0
Accept: */*
Accept-Encoding: gzip,deflate,br
Connection: Keep-alive
Host: www.domain.com
User-Agent: APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)
X-Goog-Channel-Expiration: Thu, 29 Dec 2016 00:00:00 GMT
X-Goog-Channel-Id: 01ecb23c-e718-8674-6ab3-623931741334
X-Goog-Message-Number: 2745870
X-Goog-Resource-Id: hw75x654x56jYhRNkfU5CFEXXXhtlj8
X-Goog-Resource-State: change
X-Goog-Resource-Uri: https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=658&restrictToMyDrive=false&spaces=drive&alt=json

根据this documentation,有一些包含请求正文的“更改”通知消息。不幸的是,我无法获得请求正文。

处理推送通知的脚本有以下逻辑:

$oldcontent = file_get_contents('notifications.txt');

$newnotsfile = fopen("notifications.txt", "w");

$post = file_get_contents('php://input');
$requestBody = json_decode($post , TRUE); //convert JSON into array

$time = date("Y-M-d H:i:s", time());
fwrite($newnotsfile , "<br><br>---------------- │ Time: ".$time."<br><br>");

foreach (getallheaders() as $name => $value) {
    fwrite($newnotsfile , $name.": ".$value."<br>");
} 

fwrite($newnotsfile , $requestBody );
fwrite($newnotsfile , "<br><br>");

fwrite($newnotsfile , $oldcontent);
fclose($newnotsfile );

?>

我认为通过使用 $post = file_get_contents('php://input'); 我会捕获请求正文,但事实是它什么也没捕获。如果我理解正确,我应该会收到一个结构详细为herechange 资源。我做错了什么还是我理解错了?感谢您提供任何见解,并提前致谢!

【问题讨论】:

  • 我也有这个问题。你能解决这个问题吗?
  • 我没有。我不知道在哪里可以得到这方面的指导。如果您能找到答案,请发布此问题的答案。
  • 我也在这里问了一个问题。如果我找到解决方案,我会告诉你
  • 我已经回答了这个问题。让我知道您是否有任何澄清

标签: php push-notification google-api google-drive-api google-api-php-client


【解决方案1】:

实际上,webhook 通知中没有发送请求正文。因此,一旦更改到达回调 url,将通过向更改资源 uri 发出获取请求来获取更改,如下所示

Resource URI : https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=895&restrictToMyDrive=false&spaces=drive&alt=json

或者可以使用以下代码以编程方式获取更改

String pageToken = channelInfo.getCurrPageToken();
            List<Change> changes = service.changes().list(pageToken)
                    .execute().getChanges();

Google 推送通知文档本可以清楚地提到这一点,而不是提到请求正文中的更改是造成混淆的原因

【讨论】:

  • 非常感谢您回答这个问题。这很有意义。我会将您提供的代码 sn-p 改编为 PHP 语法。这解决了所有的谜。
【解决方案2】:

您可能需要查看文档 - Push Notifications,该文档描述了如何使用推送通知在资源更改时通知您的应用程序。

观看回复

如果 watch 请求成功创建通知通道,则返回 HTTP 200 OK 状态码。

监视响应的消息正文提供了有关您刚刚创建的通知通道的信息,如下例所示。

{
  "kind": "api#channel",
  "id": "01234567-89ab-cdef-0123456789ab"", // ID you specified for this channel.
  "resourceId": "o3hgv1538sdjfh", // ID of the watched resource.
  "resourceUri": "https://www.googleapis.com/drive/v3/files/o3hgv1538sdjfh", // Version-specific ID of the watched resource.
  "token": "target=myApp-myFilesChannelDest", // Present only if one was provided.
  "expiration": 1426325213000, // Actual expiration time as Unix timestamp (in ms), if applicable.
}

如果您将检查了解通知消息格式:

文件和更改的通知消息为空。

文档还提供了示例:

文件资源的更改通知消息,不包括请求正文:

POST https://example.com/notifications // Your receiving URL.
Content-Type: application/json; utf-8
Content-Length: 0
X-Goog-Channel-ID: 4ba78bf0-6a47-11e2-bcfd-0800200c9a66
X-Goog-Channel-Token: 398348u3tu83ut8uu38
X-Goog-Channel-Expiration: Tue, 19 Nov 2013 01:13:52 GMT
X-Goog-Resource-ID:  ret08u3rv24htgh289g
X-Goog-Resource-URI: https://www.googleapis.com/drive/v3/files/ret08u3rv24htgh289g
X-Goog-Resource-State:  update
X-Goog-Changed: content,properties
X-Goog-Message-Number: 10

更改资源的更改通知消息,其中包括请求正文:

POST https://example.com/notifications // Your receiving URL.
Content-Type: application/json; utf-8
Content-Length: 118
X-Goog-Channel-ID: 8bd90be9-3a58-3122-ab43-9823188a5b43
X-Goog-Channel-Token: 245t1234tt83trrt333
X-Goog-Channel-Expiration: Tue, 19 Nov 2013 01:13:52 GMT
X-Goog-Resource-ID:  ret987df98743md8g
X-Goog-Resource-URI: https://www.googleapis.com/drive/v3/changes
X-Goog-Resource-State:  changed
X-Goog-Message-Number: 23

{
  "kind": "drive#changes"
}

了解 Drive API 通知事件

本部分详细介绍了在将推送通知与 Drive API 一起使用时可以收到的通知消息。

您可以在Push Notifications Playground 尝试以下任何活动或从GitHub 下载源代码。

希望这些信息对您有所帮助。

【讨论】:

  • 谢谢。感谢您的帮助努力。我已经查看了文档上的信息,您似乎在此处复制并粘贴了这些信息。您是否能够获得“更改资源的更改通知消息,其中包括请求正文”?如果是这样,如果您能提供示例或指导我们,我们将不胜感激。谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-23
相关资源
最近更新 更多