[英]Download url not working in Google Drive API PHP
我在使用Google Drive API时遇到问题,我可以使用API来获取文件,但无法通过此链接下载。 我想,必须进行一些身份验证,但是我使用了刷新令牌进行身份验证。请参见下面的代码
$this->load->library('google-api-php-client/src/Google_Client');
include APPPATH . '/libraries/google-api-php-client/src/contrib/Google_DriveService.php';
// Library Files Configuration to get access token and Refresh Token
$client = new Google_Client();
$client->setAccessType('offline'); // default: offline
$client->setApplicationName('xxx'); //name of the application
$client->setClientId('yyyy'); //insert your client id
$client->setClientSecret('zzz'); //insert your client secret
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$client->refreshToken($drive_data->refreshtoken);
$client->getAccessToken();
$parameters = array();
$files = $service->files->listFiles($parameters);
foreach ($files['items'] as $key => $items)
{
<a href="<?php echo $files['items'][$key]['downloadUrl']; ?>">Download</a>
}
有人知道如何通过身份验证获取下载网址吗?
答案是: (Java)下载URL不起作用
GDrive v2似乎有所更改,您可能不得不使用“ webContentLink”来获取下载链接,而不是使用“ downloadUrl”
我今天遇到了同样的问题,只是为我的案子找到了解决方案。 我希望这可以帮助那些也没有得到downloadUrl的困惑的PHP编码器。 我假设您正在使用v2 API的示例,如https://developers.google.com/drive/v2/reference所示 。
首先,我更改了磁头,使其不仅可以访问元数据,还可以完全访问(请注意DRIVE常量):
<?php
require 'vendor/autoload.php';
const DRIVE = "https://www.googleapis.com/auth/drive";
define('APPLICATION_NAME', 'MAGOS poller');
define('CREDENTIALS_PATH', 'credentials.json');
define('CLIENT_SECRET_PATH', 'client_secret.json');
define('SCOPES', implode(' ', array(Google_Service_Drive::DRIVE)));
然后,我删除了我的凭据文件 (credentials.json)并重新运行了脚本,以便针对gDrive再次对其进行身份验证并重新创建了凭据文件。 之后
$downloadUrl = $file->getDownloadUrl();
终于像魅力一样工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.