The photos space will sunset in early January 2018. Your users can continue to access Google Photos via the drive space by enabling the Google Photos folder in My Drive in the Drive client settings

Detect Changes

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.

Retrieving changes requires a pageToken to indicate a point in time to fetch changes from.

Retrieving 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

StartPageToken response = driveService.changes()
    .getStartPageToken().execute();
System.out.println("Start token: " + response.getStartPageToken());

Python

response = drive_service.changes().getStartPageToken().execute()
print 'Start token: %s' % response.get('startPageToken')

PHP

$response = $driveService->changes->getStartPageToken();
printf("Start token: %s\n", $response->startPageToken);

.NET

var response = driveService.Changes.GetStartPageToken().Execute();
Console.WriteLine("Start token: " + response.StartPageTokenValue);

Ruby

response = drive_service.get_changes_start_page_token
puts "Start token: #{response.start_page_token}"

Node.js

drive.changes.getStartPageToken({}, function (err, res) {
  console.log('Start token:', res.startPageToken);
});

Retrieving 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

// Begin with our last saved start token for this user or the
// current token from getStartPageToken()
String pageToken = savedStartPageToken;
while (pageToken != null) {
  ChangeList changes = driveService.changes().list(pageToken)
      .execute();
  for (Change change : changes.getChanges()) {
    // Process change
    System.out.println("Change found for file: " + change.getFileId());
  }
  if (changes.getNewStartPageToken() != null) {
    // Last page, save this token for the next polling interval
    savedStartPageToken = changes.getNewStartPageToken();
  }
  pageToken = changes.getNextPageToken();
}

Python

# Begin with our last saved start token for this user or the
# current token from getStartPageToken()
page_token = saved_start_page_token;
while page_token is not None:
    response = drive_service.changes().list(pageToken=page_token,
                                            spaces='drive').execute()
    for change in response.get('changes'):
        # Process change
        print 'Change found for file: %s' % change.get('fileId')
    if 'newStartPageToken' in response:
        # Last page, save this token for the next polling interval
        saved_start_page_token = response.get('newStartPageToken')
    page_token = response.get('nextPageToken')

PHP

# Begin with our last saved start token for this user or the
# current token from getStartPageToken()
$pageToken = $savedStartPageToken;
while ($pageToken != null) {
    $response = $driveService->changes->listChanges($pageToken, array(
        'spaces' => 'drive'
    ));
    foreach ($response->changes as $change) {
        // Process change
        printf("Change found for file: %s", $change->fileId);
    }
    if ($response->newStartPageToken != null) {
        // Last page, save this token for the next polling interval
        $savedStartPageToken = $response->newStartPageToken;
    }
    $pageToken = $response->nextPageToken;
}

.NET

// Begin with our last saved start token for this user or the
// current token from GetStartPageToken()
string pageToken = savedStartPageToken;
while (pageToken != null)
{
    var request = driveService.Changes.List(pageToken);
    request.Spaces = "drive";
    var changes = request.Execute();
    foreach (var change in changes.Changes)
    {
        // Process change
        Console.WriteLine("Change found for file: " + change.FileId);
    }
    if (changes.NewStartPageToken != null)
    {
        // Last page, save this token for the next polling interval
        savedStartPageToken = changes.NewStartPageToken;
    }
    pageToken = changes.NextPageToken;
}

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

var pageToken;
// Using the npm module 'async'
async.doWhilst(function (callback) {
  drive.changes.list({
    pageToken: pageToken,
    fields: '*'
  }, function (err, res) {
    if (err) {
      callback(err);
    } else {
      // Process changes
      res.changes.forEach(function (change) {
        console.log('Change found for file:', change.fileId);
      });
      pageToken = res.nextPageToken;
      callback(res.newStartPageToken);
    }
  });
}, function () {
  return !!pageToken
}, function (err, newStartPageToken) {
  console.log('Done fetching changes');
  // Save the token (newStartPageToken)
});

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 Retrieving changes.

发送以下问题的反馈:

此网页
Drive REST API
Drive REST API