我已经能够建立一个频道,通过使用此处描述的方法Not receiving webhook notification from drive, why?从Google云端硬盘接收推送通知。我收到通知,一切正常。我的问题是,当我收到推送通知时,我只收到这些信息:
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');
我会捕获请求体,但事实是它没有捕获任何内容。如果我理解正确,我应该收到详细here结构的更改资源。我做错了有什么不对,或者我理解错了吗?我感谢任何可以给予的洞察力并提前感谢!
答案 0 :(得分:2)
实际上没有在webhook通知中发送的请求正文。因此,只要更改到达回调网址,就会通过发送更改资源的获取请求来获取更改,如下所示
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推送通知文档可以清楚地提到这一点,而不是提及请求正文中出现的更改,这是混淆的原因
答案 1 :(得分:1)
您可能需要查看文档 - Push Notifications,其中介绍了如何使用推送通知,以便在资源发生变化时通知您的应用。
如果监视请求成功创建了通知通道,则会返回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推送通知时可以收到的通知消息的详细信息。
您可以在Push Notifications Playground尝试以下任何事件,或从GitHub下载来源。
希望这些信息有所帮助。