google-drive-apichangelist

Google Drive API to identify the changes in files


I have a list of files and folder stored on a google drive folder. My objective is to identify any changes (modify/create/delete) related to any file/folder within a particular google drive folder i.e get the list of these file name and their drive path.

I have not registered any domain as our spring boot application is deployed on K8's pod and I do not want any notification on any domain or any webhook address. I only want the list of files being modified/deleted when I call the below method/api

Below is the code snippet for spring boot application.

            @Component
            public class DriveServiceProvider {

                @Autowired
                private DriveConfig dc;

                public Drive getDriveService() throws IOException, GeneralSecurityException {
                    
                        final GoogleCredentials credentials = GoogleCredentials
                                .fromStream(new ByteArrayInputStream(new JSONObject(dc.getCred()).toString().getBytes()))
                                .createScoped(Collections.singletonList("https://www.googleapis.com/auth/drive"));
                        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
                        return new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), requestInitializer)
                                .setApplicationName(ApplicationConstants.APPLICATION_NAME).build();
                }
            }

Controller:

            @RestController
            @RequestMapping("/admin/watch")
            public class TestGoogleWatchController {

                @Autowired
                private DriveServiceProvider driveServiceProvider;

                @PostMapping(value = "/googleWatchNotification")
                @ResponseStatus(HttpStatus.OK)
                public Channel getNotification() throws IOException, GeneralSecurityException {
                    try {
                        StartPageToken pageToken = driveServiceProvider.getDriveService().changes().getStartPageToken().execute();

                        String savedStartPageToken = pageToken.getStartPageToken();
                        System.err.println(" savedStartPageToken "+savedStartPageToken );

                        while (savedStartPageToken != null) {
                            ChangeList changes = driveServiceProvider.getDriveService().changes().list(savedStartPageToken).execute();
                            System.err.println("======= changes " + changes.toPrettyString());
                            System.err.println("============ size ::" + changes.getChanges().size());
                            System.err.println("============ ChangeList ::" + changes.getChanges());

                            for (Change changeObj : changes.getChanges()) {
                                System.err.println(" files "+changeObj.getFileId() + " Kind "+changeObj.getKind() + ", Team Drive ID "+changeObj.getTeamDriveId() + ", Type::"+changeObj.getType()+ ",File ::"+changeObj.getFile()+ ", Is Removed::"+changeObj.getRemoved()+ ",Time ::"+changeObj.getTime());
                            }

                        }
                    }catch(Exception ex) {

                    }
                    return null;
                }
            }

*Below is the output after executing:

savedStartPageToken 165

======= changes { "changes" : [ ], "kind" : "drive#changeList", "newStartPageToken" : "165" }

============ size ::0

============ ChangeList ::[]*

The savedStartPageToken is printing incrementing numeric value after we change anything in drive, however there the ChangeList is always blank.


Solution

  • I am glad that I am answering my own question. The issue was that I was passing the current token in the changeList api.

    Actually, the solution is to find the changes since last token till the current token using a loop.