For Google Drive apps that need to keep track of changes to files, the Changes collection provides an efficient way to detect changes to all files, including those that have been shared with a user. The collection works by providing the current state of each file, if and only if the file has changed since a given point in time.
Retrieve the start page token for the first time
To request the page token for the current state of the account, call changes.getStartPageToken. Store and use this token in the initial call to changes.list.
To retrieve the current page token:
Java
Python
PHP
.NET
Ruby
response = drive_service.get_changes_start_page_token
puts "Start token: #{response.start_page_token}"
Node.js
Retrieve changes
To retrieve the list of changes for the currently authenticated user, send a
GET
request to the changes collection, as detailed in the
List reference.
Entries in the changes collection are ordered in chronological order. That is,
the oldest changes show up first. The includeRemoved
and
restrictToMyDrive
query parameters of the changes collection
decide whether the response should include removed or shared items,
respectively.
Java
Python
PHP
.NET
Ruby
# Begin with our last saved start token for this user or the
# current token from get_changes_start_page_token()
page_token = saved_start_page_token;
while page_token do
response = drive_service.list_changes(page_token,
spaces: 'drive')
for change in response.changes
# Process change
puts "Change found for file: #{change.file_id}"
end
if response.new_start_page_token
# Last page, save this token for the next polling interval
saved_start_page_token = response.new_start_page_token
end
page_token = response.next_page_token
end
Node.js
The retrieved changes collection may or may not contain a nextPageToken
. If
the nextPageToken
is present, it may be used to gather the next set of
changes. If the nextPageToken
is not present, the client application should
store the nextStartPageToken
in the collection for future use. With the
page token stored, the client application is prepared to query again for
changes in the future.
Using push notifications
Use the changes.watch
method to
subscribe for updates to the change log. Notifications do not contain details
about the changes. Instead, they indicate that new changes are available.
To retrieve the actual changes, poll the change feed as described
in Retrieve changes.