簡體   English   中英

下載網址無法在Google Drive API PHP中使用

[英]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,您需要獲取文件的元數據。 您可以使用get方法來執行此操作。 該方法將返回文件資源表示形式。 在此資源中,有一個downloadUrl屬性。 如果您能夠訪問文件並已經獲得URL,則身份驗證設置應該沒有問題。 可能存在權限問題,您可能無權訪問某些驅動器文件,但是如果沒有收到錯誤消息,則在那里也應該沒事。 我對PHP不是特別熟悉,但是也許您沒有正確下載它? 在這里 ,似乎采取了不同的做法。

我還建議您查看Drive PHP 快速入門應用程序以用作參考。

我今天遇到了同樣的問題,只是為我的案子找到了解決方案。 我希望這可以幫助那些也沒有得到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM