I wrote the following function to check if a folder exists in Google Drive. It works normally but if the folder is in shared drives it doesn't work. How can I do?
public function ExistsFolderOnDrive($access_token, $folder_name, $parent_folder_id) {
$query = "mimeType='application/vnd.google-apps.folder' and trashed=false and name='$folder_name' and '$parent_folder_id' in parents";
$apiURL = self::DRIVE_FILES_URI . "?q=" . urlencode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200) {
$error_msg = 'Failed to retrieve folder from Google Drive';
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
throw new Exception('Error ' . $http_code . ': ' . $error_msg);
}
if (empty($data['files'])) {
// The folder doesn't exist
return false;
} else {
// Folder exists, return folder information
return $data['files'][0]['id'];
}
}
I tried adding
'sharedWithMe' in parents
or even
and ('$parent_folder_id' in parents or 'sharedWithMe' in parents)
to the query but it doesn't work. I'm looking for some suggestions on how to do it
About your following question,
It works normally but if the folder is in shared drives it doesn't work. How can I do?
In the case of your showing script, in order to search the shared drive, how about modifying it as follows?
$apiURL = self::DRIVE_FILES_URI . "?q=" . urlencode($query);
$apiURL = self::DRIVE_FILES_URI . "?q=" . urlencode($query) . "&corpora=allDrives&includeItemsFromAllDrives=true&supportsAllDrives=true";