简体   繁体   English

下载网址无法在Google Drive API PHP中使用

[英]Download url not working in Google Drive API PHP

I am having issue with the Google Drive API, i was able to fetch the files using API But i can't download via this link. 我在使用Google Drive API时遇到问题,我可以使用API​​来获取文件,但无法通过此链接下载。 I guess, must some auth, but i have used refresh tokens to authenticate.Please see below for my code 我想,必须进行一些身份验证,但是我使用了刷新令牌进行身份验证。请参见下面的代码

$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>
}

Anybody knows how to get the download url with authentication? 有人知道如何通过身份验证获取下载网址吗?

This is having the answer: (Java) Download URL not working 答案是: (Java)下载URL不起作用

There seem to be some changes on v2 of GDrive, instead of using "downloadUrl" you may have to use "webContentLink" for getting the download link GDrive v2似乎有所更改,您可能不得不使用“ webContentLink”来获取下载链接,而不是使用“ downloadUrl”

To get downloadUrls, you need to get the metadata of a file. 要获取downloadUrl,您需要获取文件的元数据。 You can do so by using the get method. 您可以使用get方法来执行此操作。 The method will return a File Resource representation. 该方法将返回文件资源表示形式。 In this resource, there is a downloadUrl property. 在此资源中,有一个downloadUrl属性。 If you're able to access the files and get the URL already, then there should be no problem with your authentication setup. 如果您能够访问文件并已经获得URL,则身份验证设置应该没有问题。 There could be permission issues where you may not have access to certain drive files, but if you receive no error for it, you should be fine there too. 可能存在权限问题,您可能无权访问某些驱动器文件,但是如果没有收到错误消息,则在那里也应该没事。 I am not particularly familiar with PHP, but perhaps you are not downloading it correctly? 我对PHP不是特别熟悉,但是也许您没有正确下载它? Here it seems to be done differently. 在这里 ,似乎采取了不同的做法。

I also suggest that you check out the Drive PHP Quickstart App to use as a reference. 我还建议您查看Drive PHP 快速入门应用程序以用作参考。

I have bumped into the same problem today and just found a solution for my case. 我今天遇到了同样的问题,只是为我的案子找到了解决方案。 I hope that this helps the one or another confused PHP coder out there who also does not get a downloadUrl. 我希望这可以帮助那些也没有得到downloadUrl的困惑的PHP编码器。 I assume that you are working with the examples of the v2 API, as seen on https://developers.google.com/drive/v2/reference . 我假设您正在使用v2 API的示例,如https://developers.google.com/drive/v2/reference所示

First, I have altered the head to not only access the metadata but get full access (mind the DRIVE constant): 首先,我更改了磁头,使其不仅可以访问元数据,还可以完全访问(请注意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)));

Then I have deleted my credentials file (credentials.json) and reran the script so it authenticated once more against gDrive and recreated the credentials file. 然后,我删除了我的凭据文件 (credentials.json)并重新运行了脚本,以便针对gDrive再次对其进行身份验证并重新创建了凭据文件。 After that 之后

$downloadUrl = $file->getDownloadUrl();

finally worked like a charm. 终于像魅力一样工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM