问题描述
通过使用此处介绍的方法,我已经能够建立一个通道接收来自Google云端硬盘的推送通知。我收到通知,一切正常。我的问题是,当我收到推送通知时,我只能得到以下信息:
内容长度:0
接受:* / *
接受编码:gzip,deflate,br
连接:保持活跃
主机:www.domain.com
用户代理:APIs-Google ; (+ https://developers.google.com/webmasters/APIs-Google.html)
X-Goog-Channel-Expiration:2016年12月29日星期四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-状态:更改
X-Goog-Resource-Uri:https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=658&restrictToMyDrive=false&spaces=drive& ; alt = json
根据中,有一些包含请求正文的更改通知消息。不幸的是,我一直无法获得请求主体。
处理推送通知的脚本具有以下逻辑:
$ oldcontent = file_get_contents('notifications.txt');
$ newnotsfile = fopen(notifications.txt,w);
$ post = file_get_contents('php:// input');
$ requestBody = json_decode($ post,TRUE); //将JSON转换为数组
$ time = date(Y-M-d H:i:s,time());
fwrite($ newnotsfile,< br>< br> ----------------│时间:。$ 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://输入');
我会捕获请求主体,但事实是它没有捕获任何东西。如果我理解正确,我会收到更改资源,其结构详细信息。我在做什么错,或者我明白了这个错误?我很欣赏任何可以提供的洞察力,并提前感谢!
实际上,没有请求正文在webhook中发送通知。所以,一旦更改到达回调url,就会通过发出get请求来改变资源uri,如下所示,以获取更改
资源URI:https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=895&restrictToMyDrive=false&spaces=drive&alt=json
或者使用下面的代码获取编程式更改
String pageToken = channelInfo.getCurrPageToken();
列表<更改>更改= service.changes()。list(pageToken)
.execute()。getChanges();
Google推送通知文档可能已经清楚地提到了这一点,而不是提到请求主体中出现的变化这是造成混乱的原因。
I have been able to establish a channel to receive push notifications from Google Drive by using the method described here Not receiving webhook notification from drive, why?. I am receiving notifications and everything is working fine. My problem is that when I receive the push notifications, I am only getting this information:
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
According to this documentation, there are some "Change" notifications messages that include a request body. Unfortunately, I have not been able to get the request body.
The script that handles the push notifications has the following logic:
$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 );
?>
I thought that by using $post = file_get_contents('php://input');
I would capture the request body but the truth is that it is capturing nothing. If I understand correct, I should receive a change resource with the structure detailed here. Is there something wrong that I'm doing or have I understood this wrong? I appreciate any insight that can be given and thanks in advance!
Actually there is no request body which gets sent in the webhook notification. So as soon as changes arrive in the callback url, changes are to be fetched by making a get request to changes resource uri like below
Resource URI : https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=895&restrictToMyDrive=false&spaces=drive&alt=json
Or programatically changes can be fetched by using the below code
String pageToken = channelInfo.getCurrPageToken();
List<Change> changes = service.changes().list(pageToken)
.execute().getChanges();
Google push notifications doc could have mentioned this clearly rather than mentioning that the changes come along in the request body which is the reason for confusion
这篇关于接收Google云端硬盘推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!