[英]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.