0

I have created two watches on two folders in my shared drives. I want to be notified if someone adds or updates any file in the folders.

It was working for about 1 month. Now I noticed, the Google does not hit my URL if something changed in the folders. I have tried to add a file, delete a file, change the name of a file and nothing triggers the change event.

Google hits my URL only if I'm renewing (creating new) watch on the folders. Then I don't know about API until I renew again.

I use PHP lib:

public function createFileWatch(string $fileId, ?string $token = NULL): Google_Service_Drive_Channel
{
    $optParams = [
        'fields' => '*',
        'supportsAllDrives' => TRUE,
    ];

    $channel = new Google_Service_Drive_Channel();
    $channel->setId(Random::generate());
    $channel->setType('web_hook');
    $channel->setAddress(self::WATCH_REQUEST_URL);
    $channel->setExpiration((new DateTime('+1 week'))->getTimestamp() * 1000);
    if ($token) {
        $channel->setToken($token);
    }

    return $this->drive->files->watch($fileId, $channel, $optParams);
}
MakoBuk
  • 622
  • 1
  • 10
  • 19
  • Did you get a confirmation that the channel was successfully set up? do you have a uuid? – Aerials Mar 04 '21 at 08:44
  • @Aerials Yes, the UUID is the ID generate by (Random::generate()). I am receiving 200 and **Google_Service_Drive_Channel** with the ID and other data. It seems to me the watch channel is successfully created, but it will never trigger. – MakoBuk Mar 04 '21 at 09:53
  • Does the channel not trigger for a particular file or for any file for which you create a subscription? – Aerials Mar 12 '21 at 17:00

2 Answers2

2

You have to use changes.watch instead of files.watch API because shared drives have different changes collections than user-specific ones: https://developers.google.com/drive/api/v3/about-changes#track_shared_drives

I had the same problem and solved it with the following code (written in TypeScript for NodeJS in my case).

const drive = google.drive({ version: "v3" });
const channelId = uuidv4();
const fileId = '<FileID to watch>'

const startPageTokenRes = await drive.changes.getStartPageToken({
  driveId: "<Shared Drive ID>",
  supportsAllDrives: true,
  supportsTeamDrives: true, // This seems to be necessary even though it's deprecated. If not provided explicitly, it seems to be set as `false` under the hood. NodeJS lib's bug?
});
const startPageToken = startPageTokenRes.data.startPageToken;
if (startPageToken == null) {
  throw new Error("startPageToken is unexpectedly null");
}
const res = await drive.changes.watch({
  supportsAllDrives: true,
  supportsTeamDrives: true, // This seems to be necessary even though it's deprecated. If not provided explicitly, it seems to be set as `false` under the hood. NodeJS lib's bug?
  pageToken: startPageToken,
  requestBody: {
    kind: "api#channel",
    id: channelId,
    resourceId: fileId,
    type: "web_hook",
    address: "https://<My domain>/webhook",
  },
});

FYI: I had this error with the following code. I think this code does the same as your PHP one.

const drive = google.drive({ version: "v3", auth: oAuth2Client });
const channelId = uuidv4();
const fileId = '<FileID>'

const res = await drive.files.watch({
  fileId: fileId,
  supportsAllDrives: true,
  supportsTeamDrives: true,
  requestBody: {
    id: channelId,
    type: "web_hook",
    address: "https://<My domain>/webhook",
  },
});
whitphx
  • 56
  • 3
-2

You watch a single resource. So if you are adding a watch to a folder id, then you change the name of the folder it should notify you that a change was made to the folder.

It's not going to notify you of changes to files in the folder for that you would need to add a watch on each of the files in the folder.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I don't think so. It worked for about a month and notify me if something changed in the folder. Now, I tried to create watch to single file in different drive and it also does not notify me. There is something wrong in global scope. – MakoBuk Mar 03 '21 at 14:34