gpt4 book ai didi

google-api - 带有 PHP 客户端 2.0 的 Google Drive API V3 - 在单个请求中下载文件内容和元数据

转载 作者:行者123 更新时间:2023-12-01 15:39:03 28 4
gpt4 key购买 nike

我正在尝试使用带有 Drive API V3 的 PHP 客户端 v2.0 从 Google Drive 下载文件。

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

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

如果我不提供 'alt=media',则会返回包含所有元数据但没有正文的 Google_Service_Drive_DriveFile

问题。

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

最佳答案

尝试这样的事情:

<?php

//先看这个:https://developers.google.com/api-client-library/php/auth/web-app

require_once __DIR__ . '/vendor/autoload.php';

//更改为您的时区 date_default_timezone_set('太平洋/奥克兰');

$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setIncludeGrantedScopes(true);

//将范围更改为您需要的范围

$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);

//如果token过期则刷新token。谷歌在必要的一小时后过期

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());
?>

关于google-api - 带有 PHP 客户端 2.0 的 Google Drive API V3 - 在单个请求中下载文件内容和元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35616118/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com