Reputation: 131
I have a problem with google drive.
Before the link:
https://drive.google.com/uc?export=download&id=FILE_ID
Worked fine until now, :-( but now it does not work anymore. The result is not found error 404, but the file exists. I could upload file and easily build to download url.
For example this, public document.
https://drive.google.com/uc?export=download&id=1Hz4D52yKS0qPIUrMmeYzEl_16UpdPrGhJAwSV_EaM4Y
The document exist.
https://docs.google.com/document/d/1Hz4D52yKS0qPIUrMmeYzEl_16UpdPrGhJAwSV_EaM4Y/edit
How can I download files without authorization, i need direct link.
Thanks!!!
Upvotes: 4
Views: 4852
Reputation: 131
My solution:
require_once '../autoload.php';
$user_to_impersonate = 'XXXXXXXXX';
putenv('GOOGLE_APPLICATION_CREDENTIALS='. __DIR__ .'/service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client_email = '[email protected]';
$client->setSubject($client_email);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$driveservice = new Google_Service_Drive($client);
//var_dump($driveservice);
$parameters = array();
if (isset($pageToken)) {
$parameters['pageToken'] = $pageToken;
}
$files = $driveservice->files->listFiles($parameters);
$fileId = 'XXXXXXXXXXXXXXXXXXXX';
$file = $driveservice->files->export($fileId, 'application/pdf', array(
'alt' => 'media' ));
$size = $file->getBody()->getSize();
$content = $file->getBody()->read($size);
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"XXXX.pdf\"");
echo $content;
Upvotes: 0
Reputation: 5601
The download url works for files in Drive. e.g. https://docs.google.com/uc?id=FILE_ID
The problem is that you are trying to download a "Google document" not a file.
To download a Google document you can use the File export
method. Typically, you can download the Google doc in any of the formats listed in the UI menu File > Download as...
options.
Upvotes: 7