下载公共谷歌驱动器文件

时间:2017-02-01 12:39:51

标签: google-drive-api

我的谷歌驱动器有问题。

在链接之前:

https://drive.google.com/uc?export=download&id=FILE_ID

到目前为止工作正常,:-(但现在它不再起作用。结果找不到错误404,但文件存在。我可以上传文件并轻松构建下载网址。

例如,公共文件。

https://drive.google.com/uc?export=download&id=1Hz4D52yKS0qPIUrMmeYzEl_16UpdPrGhJAwSV_EaM4Y

该文件存在。

https://docs.google.com/document/d/1Hz4D52yKS0qPIUrMmeYzEl_16UpdPrGhJAwSV_EaM4Y/edit

如何在未经授权的情况下下载文件,我需要直接链接。

感谢!!!

2 个答案:

答案 0 :(得分:5)

下载网址适用于云端硬盘中的文件。例如https://docs.google.com/uc?id=FILE_ID

问题是您正在尝试下载" Google文档"不是文件。

要下载Google文档,您可以使用File export方法。通常,您可以使用UI菜单File > Download as...选项中列出的任何格式下载Google文档。

答案 1 :(得分:0)

我的解决方案:



require_once '../autoload.php';
$user_to_impersonate = 'XXXXXXXXX';
putenv('GOOGLE_APPLICATION_CREDENTIALS='. __DIR__ .'/service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client_email   = 'XXXX@XXXX.com'; 
$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;




相关问题