接收 Google 驱动器推送通知
Receiving Google Drive Push Notifications
我已经能够使用此处描述的方法 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,有一些 "Change" 通知消息包含 请求正文 。不幸的是,我无法获得请求正文。
处理推送通知的脚本具有以下逻辑:
$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 的 change 资源。我做错了什么或者我理解错了吗?感谢您提供的任何见解,并提前致谢!
您可能需要查看文档 - Push Notifications,它描述了如何使用推送通知在资源更改时通知您的应用程序。
观察反应
If the watch request successfully creates a notification channel, it returns an HTTP 200 OK
status code.
The message body of the watch response provides information about the notification channel you just created, as shown in the example below.
{
"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.
}
如果您要查看了解通知消息格式:
Notification messages for Files and Changes are empty.
文档还提供了样本:
文件资源的更改通知消息,不包含请求正文:
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"
}
了解驱动器API 通知事件
This section provides details on the notification messages you can receive when using push notifications with the Drive API.
You can try out any of the events below at the Push Notifications Playground or download the source from GitHub.
希望这些信息对您有所帮助。
实际上,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 推送通知文档本可以清楚地提到这一点,而不是提到请求正文中出现的更改,这是造成混淆的原因
我已经能够使用此处描述的方法 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,有一些 "Change" 通知消息包含 请求正文 。不幸的是,我无法获得请求正文。
处理推送通知的脚本具有以下逻辑:
$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 的 change 资源。我做错了什么或者我理解错了吗?感谢您提供的任何见解,并提前致谢!
您可能需要查看文档 - Push Notifications,它描述了如何使用推送通知在资源更改时通知您的应用程序。
观察反应
If the watch request successfully creates a notification channel, it returns an HTTP
200 OK
status code.The message body of the watch response provides information about the notification channel you just created, as shown in the example below.
{
"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.
}
如果您要查看了解通知消息格式:
Notification messages for Files and Changes are empty.
文档还提供了样本:
文件资源的更改通知消息,不包含请求正文:
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"
}
了解驱动器API 通知事件
This section provides details on the notification messages you can receive when using push notifications with the Drive API.
You can try out any of the events below at the Push Notifications Playground or download the source from GitHub.
希望这些信息对您有所帮助。
实际上,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 推送通知文档本可以清楚地提到这一点,而不是提到请求正文中出现的更改,这是造成混淆的原因