Receiving Google Drive Push Notifications

我是研究僧i 提交于 2019-12-01 12:31:33

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

You might want to check the documentation - Push Notifications, this describes how to use push notifications that inform your application when a resource changes.

Watch response

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.
}

And if you will check the Understanding the notification message format:

Notification messages for Files and Changes are empty.

The docs also provided samples:

Change notification message for Files resources, which does not include a request body:

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

Change notification message for Changes resources, which includes a request body:

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"
}

Understanding Drive API notification events

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.

Hope this information helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!