Google Drive SDK

Files: watch

Start watching for changes to a file.

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.

Request body

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

{
  "id": string,
  "token": string,
  "type": string,
  "address": string,
  "params": {
    "ttl": 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.
type string The type of delivery mechanism used for this channel. The only option is web_hook.
address string The address where notifications are delivered for this channel.
params object Additional parameters controlling delivery channel behavior. Optional.
params.ttl string The time-to-live in seconds for the notification channel. Default is 3600 seconds.

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. Value: the fixed string 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 {

  // ...

  /**
   * Start watching for changes to a file.
   *
   * @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_DriveService $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_Channel The created channel if successful, NULL otherwise.
 */
function watchFile($service, $fileId, $channelId, $channelType, $channelAddress) {
  $channel = new Google_Channel();
  $channel->setId($channelId);
  $channel->setType($channelType);
  $channel->setAddress($channelAddress);
  try {
    return $service->files->watch($fileId, $channel);
  } catch(Exception $e) {
    print "An error occured: " . $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

Ruby

Uses the Ruby client library

##
# Watch any changes to a file.
#
# @param [Google::APIClient] client
#   Authorized client instance.
# @param [String] file_id
#   ID of the file to watch.
# @param [String] channel_id
#   Unique string that identifies this channel.
# @param [String] channel_type
#   Type of delivery mechanism used for this channel.
# @param [String] channel_address
#   Address where notifications are delivered.
# @return [Google::APIClient::Schema::Drive::V2::Channel]
#   The created channel if successful, nil otherwise
def watch_file(client, file_id, channel_id, channel_type, channel_address)
  drive = client.discovered_api('drive', 'v2')
  result = client.execute(
    :api_method => drive.files.watch,
    :body_object => { 'id' => channel_id, 'type' => channel_type, 'address' => channel_address },
    :parameters => { 'fileId' => file_id })
  if result.status == 200
    return result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end
end

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);});
}

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.