1

I have integrated google drive into my application. To achieve sync, I have also configured push notifications for each account by following the steps in the link https://developers.google.com/drive/v3/web/push

Below is the java code to configure watch on all files for the account

String uuid = UUID.randomUUID().toString(); 
                Channel channel = new Channel(); 
                channel.setId(uuid);
                channel.setType("web_hook");
                channel.setAddress(env.getProperty("webhookUrl"));
                StartPageToken pageToken = service.changes().getStartPageToken().execute();
                Channel response = service.changes().watch(pageToken.getStartPageToken(), channel).execute();

On making changes in the actual google drive, I get the notification in the webhook url configured above.

But the problem is for every change, I am getting the same values for below headers which are same as the response of the watch call & I am not getting any proper request headers corresponding to the change or request body

//Getting request headers
    String resourceId = request.getHeader("X-Goog-Resource-ID");
    String resourceState = request.getHeader("X-Goog-Resource-State");
    String expiration = request.getHeader("X-Goog-Channel-Expiration");
    String resourceChanges = request.getHeader("X-Goog-Changed");
    String channelId = request.getHeader("X-Goog-Channel-ID");

Can someone please let me know how do i get notification data correctly ? Is there anything I am doing wrong ?

Here is the same problem stated by another question which does not have proper answer yet Receiving Google Drive Push Notifications

Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105

3 Answers3

5

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

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

Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
1

I don't understand why you are looking at the headers of the webhook message. It is the body that you are interested in as described at https://developers.google.com/drive/v3/reference/files/watch.

Once you have the ID of the changed file, if you want details of what has changed, you can use the Revisions https://developers.google.com/drive/v3/web/manage-revisions feed to see the details.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • I am not getting request body. It is coming as null for some weird reason because of which I am not able to get the id of the resource. Is this possible ? – Aarish Ramesh Feb 14 '17 at 01:20
  • I am using Spring & this is my callback post method @RequestMapping(method = RequestMethod.POST) @ApiOperation(value = "Handling webhook notifications", notes = "", response = ApiResponse.class, tags = { "" }) public void webhookNotification(HttpServletRequest request, HttpServletResponse response ) throws IOException { . When I include @RequestBody in this , I am getting error that it does not exist – Aarish Ramesh Feb 14 '17 at 01:23
  • Any reason why request body is null ? – Aarish Ramesh Feb 14 '17 at 01:37
  • I would need to see the http request and response for the subscription and also for the webhook call. Are you sure you aren't simply seeing the 'sync' message? Check for "X-Goog-Message-Number: == 1" in the headers. The sync message should simply be ignored. – pinoyyid Feb 14 '17 at 12:15
  • Header Name - x-real-ip, Value - 64.233.172.157 Header Name - x-forwarded-for, Value - 64.233.172.157, 64.233.172.157 Header Name - x-forwarded-proto, Value - http Header Name - host, Value - *** Header Name - connection, Value - close Header Name - content-length, Value - 0 Header Name - x-forwarded-host, Value - *** Header Name - x-forwarded-server, Value - *** Header Name - accept, Value - */* Header Name - x-goog-channel-id, Value - *** Header Name - x-goog-channel-expiration, Value - Tue, 14 Feb 2017 12:11:12 GMT Header Name - x-goog-resource-state, Value - change – Aarish Ramesh Feb 14 '17 at 12:25
  • Header Name - x-goog-message-number, Value - 721863 Header Name - x-goog-resource-id, Value - *** Header Name - x-goog-resource-uri, Value - *** Header Name - user-agent, Value - APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html) Header Name - accept-encoding, Value - gzip,deflate,br – Aarish Ramesh Feb 14 '17 at 12:25
  • Above is the webhook response – Aarish Ramesh Feb 14 '17 at 12:26
  • Following is the subscription response which I get KIND = api#channel : ID = *** , ResourceId : 91Crx*** , Resource URI : https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=895&restrictToMyDrive=false&spaces=drive&alt=json, Token = null , Expiration = 1487062413000" – Aarish Ramesh Feb 14 '17 at 12:28
  • Am i missing out something ? I am stuck at this thing. Also I am manually subscribing notifications because it expires in an hour. Is that a concern ? – Aarish Ramesh Feb 14 '17 at 12:32
  • I tried getting request body in many ways but it is empty. How can it be empty ? @pinnoyid – Aarish Ramesh Feb 14 '17 at 12:34
  • You are not missing anything. There is generally no request body. The push notifications will not send you a list of changes made to a resource/file, only information about what filed has changed. You use the headers to determine which resource has changed, then query whatever API (docs, sheets, etc) to get the content of the changed resource, accordingly. – erfling Mar 18 '18 at 15:18
0

Drive provides the following details in the request header.

We can use these info to fetch the file or make any appropriate application changes.

enter image description here

Mukundhan
  • 3,284
  • 23
  • 36