Google api GoogleDriveAPIv3和PHP客户端2.0-在单个请求中下载文件内容和元数据

Google api GoogleDriveAPIv3和PHP客户端2.0-在单个请求中下载文件内容和元数据,google-api,google-drive-api,google-api-php-client,Google Api,Google Drive Api,Google Api Php Client,我正在尝试使用PHP客户端v2.0和驱动器API V3从Google Drive下载文件 是否可以在单个HTTP请求中检索文件的正文和元数据 将'alt=media'提供给->files->get()将返回GuzzleHttp\Psr7\Response,我可以在其上运行->getBody()->\uu toString() 如果我没有提供'alt=media',则返回包含所有元数据但没有正文的Google\u Service\u Drive\u DriveFile 问题。 是否可以在同一个请求

我正在尝试使用PHP客户端v2.0和驱动器API V3从Google Drive下载文件

是否可以在单个HTTP请求中检索文件的正文和元数据

'alt=media'
提供给
->files->get()
将返回
GuzzleHttp\Psr7\Response
,我可以在其上运行
->getBody()->\uu toString()

如果我没有提供
'alt=media'
,则返回包含所有元数据但没有正文的
Google\u Service\u Drive\u DriveFile

问题。


是否可以在同一个请求中同时获取元数据和正文?

尝试以下操作:

<?php
//改变你的时区 日期默认时区设置(“太平洋/奥克兰”)

//将作用域更改为所需的作用域

$client->addScope(Google_Service_Drive::DRIVE_FILE, Google_Service_Drive::DRIVE_APPDATA, Google_Service_Drive::DRIVE, Google_Service_Drive::DRIVE_METADATA);
$accessToken = json_decode(file_get_contents('credentials.json'), true);
$client->setAccessToken($accessToken);
//如果令牌已过期,请刷新该令牌。谷歌在一小时后到期,这是必要的

if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents('credentials.json', json_encode($client->getAccessToken()));
}

$service = new Google_Service_Drive($client);
$results = $service->files->listFiles($optParams);
$fileId = 'yourfileid;
$file = $service->files->get($fileId, array('alt' => 'media'));
file_put_contents("hello.pdf",$file->getBody());
?>

您只能在两个单独的请求中进行。这是因为下载文件需要
alt=media
。还有另一种下载文件的方法,就是使用
downloadURL
,但使用它您将无法请求元数据。
$client->addScope(Google_Service_Drive::DRIVE_FILE, Google_Service_Drive::DRIVE_APPDATA, Google_Service_Drive::DRIVE, Google_Service_Drive::DRIVE_METADATA);
$accessToken = json_decode(file_get_contents('credentials.json'), true);
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents('credentials.json', json_encode($client->getAccessToken()));
}

$service = new Google_Service_Drive($client);
$results = $service->files->listFiles($optParams);
$fileId = 'yourfileid;
$file = $service->files->get($fileId, array('alt' => 'media'));
file_put_contents("hello.pdf",$file->getBody());
?>