Changes: watch

Stay organized with collections Save and categorize content based on your preferences.

Watch for all changes to a user's Drive.

Request

HTTP request

POST https://www.googleapis.com/drive/v2/changes/watch

Parameters

Parameter name Value Description

Authorization

This request requires authorization with at least one of the following scopes:

Scope
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.readonly
https://www.googleapis.com/auth/drive.metadata.readonly
https://www.googleapis.com/auth/drive.appdata
https://www.googleapis.com/auth/drive.apps.readonly
https://www.googleapis.com/auth/drive.metadata
https://www.googleapis.com/auth/drive.photos.readonly

Some scopes are restricted and require a security assessment for your app to use them. For more information, see the authentication and authorization page.

Request body

In the request body, supply data with the following structure:

{
  "id": string,
  "token": string,
  "expiration": long,
  "type": string,
  "address": string
}
Property name Value Description Notes
id string A UUID or similar unique string that identifies this channel.
token string An arbitrary string delivered to the target address with each notification delivered over this channel. Optional.
expiration long Date and time of notification channel expiration, expressed as a Unix timestamp, in milliseconds. Optional.
type string The type of delivery mechanism used for this channel. Valid values are "web_hook" (or "webhook"). Both values refer to a channel where Http requests are used to deliver messages.
address string The address where notifications are delivered for this channel.

Response

If successful, this method returns a response body with the following structure:

{
  "kind": "api#channel",
  "id": string,
  "resourceId": string,
  "resourceUri": string,
  "token": string,
  "expiration": long
}
Property name Value Description Notes
kind string Identifies this as a notification channel used to watch for changes to a resource, which is "api#channel".
id string A UUID or similar unique string that identifies this channel.
resourceId string An opaque ID that identifies the resource being watched on this channel. Stable across different API versions.
resourceUri string A version-specific identifier for the watched resource.
token string An arbitrary string delivered to the target address with each notification delivered over this channel. Optional.
expiration long Date and time of notification channel expiration, expressed as a Unix timestamp, in milliseconds. Optional.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Channel;

import java.io.IOException;

// ...

public class MyClass {

  // ...

  /**
   * Watch for all changes to a user's Drive.
   *
   * @param service Drive API service instance.
   * @param channelId Unique string that identifies this channel.
   * @param channelType Type of delivery mechanism used for this channel.
   * @param channelAddress Address where notifications are delivered.
   * @return The created channel if successful, {@code null} otherwise.
   */
  private static Channel watchChange(Drive service, String channelId,
      String channelType, String channelAddress) {
    Channel channel = new Channel();
    channel.setId(channelId);
    channel.setType(channelType);
    channel.setAddress(channelAddress);
    try {
      return service.changes().watch(channel).execute();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  // ...

}

PHP

Uses the PHP client library.

/**
 * Watch for all changes to a user's Drive.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param String $channelId Unique string that identifies this channel.
 * @param String $channelType Type of delivery mechanism used for this channel.
 * @param String $channelAddress Address where notifications are delivered.
 * @return Google_Service_Drive_Channel The created channel if successful, NULL otherwise.
 */
function watchChange($service, $channelId, $channelType, $channelAddress) {
  $channel = new Google_Service_Drive_Channel();
  $channel->setId($channelId);
  $channel->setType($channelType);
  $channel->setAddress($channelAddress);
  try {
    return $service->changes->watch($channel);
  } catch(Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return NULL;
}

Python

Uses the Python client library.

from apiclient import errors
# ...

def watch_change(service, channel_id, channel_type, channel_address):
  """Watch for all changes to a user's Drive.

  Args:
    service: Drive API service instance.
    channel_id: Unique string that identifies this channel.
    channel_type: Type of delivery mechanism used for this channel.
    channel_address: Address where notifications are delivered.

  Returns:
    The created channel if successful, None otherwise.
  """
  body = {
    'id': channel_id,
    'type': channel_type,
    'address': channel_address
  }
  try:
    return service.changes().watch(body=body).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None

JavaScript

Uses the JavaScript client library.

/**
 * Watch for all changes to a user's Drive.
 *
 * @param {String} channelId Unique string that identifies this channel.
 * @param {String} channelType Type of delivery mechanism used for this channel.
 * @param {String} channelAddress Address where notifications are delivered.
 */
function watchChange(channelId, channelType, channelAddress) {
  var resource = {
    'id': channelId,
    'type': channelType,
    'address': channelAddress
  };
  var request = gapi.client.drive.changes.watch({
    'resource': resource
  });
  request.execute(function(channel){console.log(channel);});
}