Files: watch

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

Subscribes to changes to a file. While you can establish a channel for changes to a file on a shared drive, a change to a shared drive file won't create a notification.

Request

HTTP request

POST https://www.googleapis.com/drive/v2/files/fileId/watch

Parameters

Parameter name Value Description
Path parameters
fileId string The ID of the file to watch.
Optional query parameters
supportsAllDrives boolean Whether the requesting application supports both My Drives and shared drives. (Default: false)
supportsTeamDrives boolean Deprecated use supportsAllDrives instead. (Default: false)

Authorization

This request allows 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.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,
  "expiration": long,
  "token": string,
  "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 {

  // ...

  /**
   * Subscribes to changes to a file. While you can establish a channel for changes to a file on a shared drive, a change to a shared drive file won't create a notification.
   *
   * @param service Drive API service instance.
   * @param fileId ID of the file to watch.
   * @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 watchFile(Drive service, String fileId,
      String channelId, String channelType, String channelAddress) {
    Channel channel = new Channel();
    channel.setId(channelId);
    channel.setType(channelType);
    channel.setAddress(channelAddress);
    try {
      return service.files().watch(fileId, channel).execute();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  // ...

}

PHP

Uses the PHP client library.

/**
 * Watch any changes to a file.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param String $fileId ID of the file to watch.
 * @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_Servie_Drive_Channel The created channel if successful, NULL otherwise.
 */
function watchFile($service, $fileId, $channelId, $channelType, $channelAddress) {
  $channel = new Google_Service_Drive_Channel();
  $channel->setId($channelId);
  $channel->setType($channelType);
  $channel->setAddress($channelAddress);
  try {
    return $service->files->watch($fileId, $channel);
  } catch(Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return NULL;
}

Python

Uses the Python client library.

from apiclient import errors
# ...

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

  Args:
    service: Drive API service instance.
    file_id: ID of the file to watch.
    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.files().watch(fileId=file_id, body=body).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None

JavaScript

Uses the JavaScript client library.

/**
 * Watch any changes to a file.
 *
 * @param {String} fileId ID of the file to watch.
 * @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 watchFile(fileId, channelId, channelType, channelAddress) {
  var resource = {
    'id': channelId,
    'type': channelType,
    'address': channelAddress
  };
  var request = gapi.client.drive.files.watch({
    'fileId': fileId,
    'resource': resource
  });
  request.execute(function(channel){console.log(channel);});
}