I'm trying to copy a file locally with Google Drive API with the following code:
$client = new Google_Client();
$client->setApplicationName('My API');
$client->setScopes([
Google_Service_Drive::DRIVE_METADATA_READONLY,
Google_Service_Drive::DRIVE_READONLY,
Google_Service_Drive::DRIVE_FILE,
]);
$client->setAuthConfig(GOOGLE_SA_JSON);
$service = new Google_Service_Drive($client);
$file = $service->files->get(GOOGLE_STRAINS_DOC_ID, ["supportsAllDrives" => true]);
$response = $service->files->export($file->getId(), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$temp_file = tempnam(sys_get_temp_dir(), 'myAPI');
file_put_contents($temp_file, $response->getBody()->getContents());
I suspect it has something to do with "supportsAllDrives" not being passed with the export function...
The response is this:
{ "error": { "code": 403, "message": "Export only supports Docs Editors files.", "errors": [ { "message": "Export only supports Docs Editors files.", "domain": "global", "reason": "fileNotExportable" } ] } }
The file is exportable and is a google "Excel" sheet,
Any help on this is appreciated.
supportsAllDrives
. RefExport only supports Docs Editors files.
, in this case, it is considered that you are trying to download a file except for Google Docs files (Document, Spreadsheet, Slides, and so on). From The file is exportable and is a google "Excel" sheet,
, is this XLSX file? In that case, such an error occurs.If you want to download both Google Docs files and other files, how about the following modification?
$file = $service->files->get(GOOGLE_STRAINS_DOC_ID, ["supportsAllDrives" => true]);
$response = $service->files->export($file->getId(), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$temp_file = tempnam(sys_get_temp_dir(), 'myAPI');
file_put_contents($temp_file, $response->getBody()->getContents());
$file = $service->files->get(GOOGLE_STRAINS_DOC_ID, ["supportsAllDrives" => true]);
$fileId = $file->getId();
$mimeType=$file->getMimeType();
echo $mimeType;
if (strpos($mimeType, "application/vnd.google-apps") !== false) {
if ($mimeType == "application/vnd.google-apps.spreadsheet") {
$response = $service->files->export($fileId, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$temp_file = tempnam(sys_get_temp_dir(), 'myAPI');
file_put_contents($temp_file, $response->getBody()->getContents());
}
} else {
$response = $service->files->get($fileId, ["supportsAllDrives" => true, "alt" => "media"]);
file_put_contents($file->getName(), $response->getBody());
}
GOOGLE_STRAINS_DOC_ID
is one of the Google Docs files and the Spreadsheet, Spreadsheet is downloaded as application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
. This is from your showing script. When the file of GOOGLE_STRAINS_DOC_ID
is not the Google Docs file, the file is directly downloaded.If you want to download only XLSX file, you can also use the following script.
$response = $service->files->get("###fileId###", ["supportsAllDrives" => true, "alt" => "media"]);
file_put_contents("filename.xlsx", $response->getBody());