简体   繁体   中英

Receiving Google Drive Push Notifications

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM