我已遵循本指南:
https://developers.google.com/drive/api/v3/quickstart/php?authuser=1
现在,我想从文件中获取下载链接。我正在尝试使用$ file-> getDownloadUrl(),但是出现以下错误:
Fatal error: Uncaught Error: Call to undefined method Google_Service_Drive_DriveFile::getDownloadUrl() in C:\xampp\htdocs\Prueba\quickstart.php:84
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\Prueba\quickstart.php on line 84
这是我的代码:
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 1,
'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", $file->getName(), $file->getId());
print($file->getDownloadUrl());
}
}
顺便说一下,客户端具有下载文件的权限,因为我已经这样定义了:
$client->setScopes(Google_Service_Drive::DRIVE_READONLY);
编辑:
最后,我已经可以使用以下代码下载PDF文件:
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
// Print the names and IDs for up to 3 files.
$optParams = array(
'pageSize' => 3,
'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", $file->getName(), $file->getId());
$fileId = $file->getId();
$fileToDown = $service->files->get($fileId, array('alt' => 'media'));
file_put_contents($file->getName(),$fileToDown->getBody());
}
}
方法getDownloadUrl()不存在。试试:
$file->downloadUrl;
您使用的方法不存在。
检查here以获取可用方法的概述。您要使用getWebContentLink()
方法。
与getWebContentLink()
webContentLink一致,现在是获取从云端硬盘下载文件的链接的方法。