我正在创建一个与Google Drive API(版本3)集成的应用程序,以便通过用户输入在指定文件夹中查找文件,然后获取该文件的Web内容链接。我的应用程序正在联系一个服务帐户,我的应用程序的文件将存储在该服务帐户中并获取链接。唯一的问题是当我使用下面显示的代码时,它返回下面显示的输出,除了几个字段外,它都是NULL。我做错了什么?
导致问题的代码:
<?php
require_once("./vendor/autoload.php");
putenv('GOOGLE_APPLICATION_CREDENTIALS=service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(implode(' ', array(Google_Service_Drive::DRIVE)));
$service = new Google_Service_Drive($client);
$optParams = array("q" => "'0BxNR85wn9MERczRqbkU0LTBQdUk' in parents");
$results = $service->files->listFiles($optParams);
if(count($results->getFiles()) == 0){
echo "No files found.";
}else{
echo "Files found.<br>";
foreach($results->getFiles() as $file){
if($file->getId() != "0B4C3vGWHN-Wgc3RhcnRlcl9maWxl"){
echo $file->getId() . "<br>";
$content = $service->files->get($file->getId());
$filedata = $service->files->get($file->getId());
echo "<pre>";
var_dump($filedata);
echo "</pre>";
}
}
}
?>
代码输出:
Files found.
1L61iP4mrc0HyVmHhLB4TFz-1revFg2l6cdPQDh7OLUY
object(Google_Service_Drive_DriveFile)#68 (56) {
["collection_key":protected]=>
string(6) "spaces"
["appProperties"]=>
NULL
["capabilitiesType":protected]=>
string(42) "Google_Service_Drive_DriveFileCapabilities"
["capabilitiesDataType":protected]=>
string(0) ""
["contentHintsType":protected]=>
string(42) "Google_Service_Drive_DriveFileContentHints"
["contentHintsDataType":protected]=>
string(0) ""
["createdTime"]=>
NULL
["description"]=>
NULL
["explicitlyTrashed"]=>
NULL
["fileExtension"]=>
NULL
["folderColorRgb"]=>
NULL
["fullFileExtension"]=>
NULL
["headRevisionId"]=>
NULL
["iconLink"]=>
NULL
["id"]=>
string(44) "1L61iP4mrc0HyVmHhLB4TFz-1revFg2l6cdPQDh7OLUY"
["imageMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileImageMediaMetadata"
["imageMediaMetadataDataType":protected]=>
string(0) ""
["isAppAuthorized"]=>
NULL
["kind"]=>
string(10) "drive#file"
["lastModifyingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["lastModifyingUserDataType":protected]=>
string(0) ""
["md5Checksum"]=>
NULL
["mimeType"]=>
string(36) "application/vnd.google-apps.document"
["modifiedByMeTime"]=>
NULL
["modifiedTime"]=>
NULL
["name"]=>
string(17) "Untitled document"
["originalFilename"]=>
NULL
["ownedByMe"]=>
NULL
["ownersType":protected]=>
string(25) "Google_Service_Drive_User"
["ownersDataType":protected]=>
string(5) "array"
["parents"]=>
NULL
["permissionsType":protected]=>
string(31) "Google_Service_Drive_Permission"
["permissionsDataType":protected]=>
string(5) "array"
["properties"]=>
NULL
["quotaBytesUsed"]=>
NULL
["shared"]=>
NULL
["sharedWithMeTime"]=>
NULL
["sharingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["sharingUserDataType":protected]=>
string(0) ""
["size"]=>
NULL
["spaces"]=>
NULL
["starred"]=>
NULL
["thumbnailLink"]=>
NULL
["trashed"]=>
NULL
["version"]=>
NULL
["videoMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileVideoMediaMetadata"
["videoMediaMetadataDataType":protected]=>
string(0) ""
["viewedByMe"]=>
NULL
["viewedByMeTime"]=>
NULL
["viewersCanCopyContent"]=>
NULL
["webContentLink"]=>
NULL
["webViewLink"]=>
NULL
["writersCanShare"]=>
NULL
["internal_gapi_mappings":protected]=>
array(0) {
}
["modelData":protected]=>
array(0) {
}
["processed":protected]=>
array(0) {
}
}
0BxNR85wn9MERUjRaYURVdXVhMlk
object(Google_Service_Drive_DriveFile)#79 (56) {
["collection_key":protected]=>
string(6) "spaces"
["appProperties"]=>
NULL
["capabilitiesType":protected]=>
string(42) "Google_Service_Drive_DriveFileCapabilities"
["capabilitiesDataType":protected]=>
string(0) ""
["contentHintsType":protected]=>
string(42) "Google_Service_Drive_DriveFileContentHints"
["contentHintsDataType":protected]=>
string(0) ""
["createdTime"]=>
NULL
["description"]=>
NULL
["explicitlyTrashed"]=>
NULL
["fileExtension"]=>
NULL
["folderColorRgb"]=>
NULL
["fullFileExtension"]=>
NULL
["headRevisionId"]=>
NULL
["iconLink"]=>
NULL
["id"]=>
string(28) "0BxNR85wn9MERUjRaYURVdXVhMlk"
["imageMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileImageMediaMetadata"
["imageMediaMetadataDataType":protected]=>
string(0) ""
["isAppAuthorized"]=>
NULL
["kind"]=>
string(10) "drive#file"
["lastModifyingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["lastModifyingUserDataType":protected]=>
string(0) ""
["md5Checksum"]=>
NULL
["mimeType"]=>
string(10) "image/jpeg"
["modifiedByMeTime"]=>
NULL
["modifiedTime"]=>
NULL
["name"]=>
string(76) "Copy of 3f86195896e5d28d5e9492eb0415544049c4a49467d3534d3fa2e5d52a2636e0.jpg"
["originalFilename"]=>
NULL
["ownedByMe"]=>
NULL
["ownersType":protected]=>
string(25) "Google_Service_Drive_User"
["ownersDataType":protected]=>
string(5) "array"
["parents"]=>
NULL
["permissionsType":protected]=>
string(31) "Google_Service_Drive_Permission"
["permissionsDataType":protected]=>
string(5) "array"
["properties"]=>
NULL
["quotaBytesUsed"]=>
NULL
["shared"]=>
NULL
["sharedWithMeTime"]=>
NULL
["sharingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["sharingUserDataType":protected]=>
string(0) ""
["size"]=>
NULL
["spaces"]=>
NULL
["starred"]=>
NULL
["thumbnailLink"]=>
NULL
["trashed"]=>
NULL
["version"]=>
NULL
["videoMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileVideoMediaMetadata"
["videoMediaMetadataDataType":protected]=>
string(0) ""
["viewedByMe"]=>
NULL
["viewedByMeTime"]=>
NULL
["viewersCanCopyContent"]=>
NULL
["webContentLink"]=>
NULL
["webViewLink"]=>
NULL
["writersCanShare"]=>
NULL
["internal_gapi_mappings":protected]=>
array(0) {
}
["modelData":protected]=>
array(0) {
}
["processed":protected]=>
array(0) {
}
}
0BxNR85wn9MERWUR1TEh6ckROUGM
object(Google_Service_Drive_DriveFile)#74 (56) {
["collection_key":protected]=>
string(6) "spaces"
["appProperties"]=>
NULL
["capabilitiesType":protected]=>
string(42) "Google_Service_Drive_DriveFileCapabilities"
["capabilitiesDataType":protected]=>
string(0) ""
["contentHintsType":protected]=>
string(42) "Google_Service_Drive_DriveFileContentHints"
["contentHintsDataType":protected]=>
string(0) ""
["createdTime"]=>
NULL
["description"]=>
NULL
["explicitlyTrashed"]=>
NULL
["fileExtension"]=>
NULL
["folderColorRgb"]=>
NULL
["fullFileExtension"]=>
NULL
["headRevisionId"]=>
NULL
["iconLink"]=>
NULL
["id"]=>
string(28) "0BxNR85wn9MERWUR1TEh6ckROUGM"
["imageMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileImageMediaMetadata"
["imageMediaMetadataDataType":protected]=>
string(0) ""
["isAppAuthorized"]=>
NULL
["kind"]=>
string(10) "drive#file"
["lastModifyingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["lastModifyingUserDataType":protected]=>
string(0) ""
["md5Checksum"]=>
NULL
["mimeType"]=>
string(10) "image/jpeg"
["modifiedByMeTime"]=>
NULL
["modifiedTime"]=>
NULL
["name"]=>
string(68) "3f86195896e5d28d5e9492eb0415544049c4a49467d3534d3fa2e5d52a2636e0.jpg"
["originalFilename"]=>
NULL
["ownedByMe"]=>
NULL
["ownersType":protected]=>
string(25) "Google_Service_Drive_User"
["ownersDataType":protected]=>
string(5) "array"
["parents"]=>
NULL
["permissionsType":protected]=>
string(31) "Google_Service_Drive_Permission"
["permissionsDataType":protected]=>
string(5) "array"
["properties"]=>
NULL
["quotaBytesUsed"]=>
NULL
["shared"]=>
NULL
["sharedWithMeTime"]=>
NULL
["sharingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["sharingUserDataType":protected]=>
string(0) ""
["size"]=>
NULL
["spaces"]=>
NULL
["starred"]=>
NULL
["thumbnailLink"]=>
NULL
["trashed"]=>
NULL
["version"]=>
NULL
["videoMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileVideoMediaMetadata"
["videoMediaMetadataDataType":protected]=>
string(0) ""
["viewedByMe"]=>
NULL
["viewedByMeTime"]=>
NULL
["viewersCanCopyContent"]=>
NULL
["webContentLink"]=>
NULL
["webViewLink"]=>
NULL
["writersCanShare"]=>
NULL
["internal_gapi_mappings":protected]=>
array(0) {
}
["modelData":protected]=>
array(0) {
}
["processed":protected]=>
array(0) {
}
}
现在它只是一堆测试代码,看看文件get方法将返回什么,目前这是很多null,所以现在不要期望太多的功能。下面的代码不应该下载任何东西,它只是调试。所以,我需要知道的是,如何访问此应用程序从API中找到的3个文件的webContentLink属性?
答案 0 :(得分:1)
请read此Drive API V3迁移文档。
默认情况下不再返回完整资源。使用fields查询参数来请求返回特定字段。如果未指定,则仅返回常用字段的子集。
答案 1 :(得分:1)
我知道已经晚了,但是我希望我能帮助别人。 我正在使用Drive V2,我将演示一个小示例,该示例按ID提取单个文件的详细信息。
首先将您的云端硬盘帐户的文件夹共享到您的服务帐户。 您的服务帐户的地址类似于XXX@XXX.iam.gserviceaccount.com。 然后,您的服务帐户可以从您的云端硬盘帐户中看到共享文件夹。
->确保您包括要显示的字段
->同时检查权限
<?php
require_once __DIR__.'/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('client_id.json');
//$client->addScope(Google_Service_Drive::DRIVE);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$driveService = new Google_Service_Drive($client);
$fileId='1gYCx2d_pmAgfPNQSR1O0cfGied';
$response = $driveService->files->get($fileId, array(
//'pageSize' => 10,
'fields' => 'id, description,webViewLink,webContentLink,properties',
//'alt' => 'media'
));
header('Content-Type: application/json'); //to beautify view in browser
echo json_encode($response);
响应
appProperties null
copyRequiresWriterPermission null
createdTime null
description "This is an audio of a recorded call"
explicitlyTrashed null
fileExtension null
folderColorRgb null
fullFileExtension null
hasAugmentedPermissions null
hasThumbnail null
headRevisionId null
iconLink null
id "1gYCx2d_pmAgfPNQSRxZcSnv1O0cfGied"
isAppAuthorized null
kind null
md5Checksum null
mimeType null
modifiedByMe null
modifiedByMeTime null
modifiedTime null
name null
originalFilename null
ownedByMe null
parents null
permissionIds null
properties
filepath "/2018-10-25/call_09-59-41_OUT_100.amr"
date "1540450781384"
recording_duration "14900"
phonenumber "100"
version "2.0"
duration "0:14"
calltype "1"
comment_subject "100"
quotaBytesUsed null
shared null
sharedWithMeTime null
size null
spaces null
starred null
teamDriveId null
thumbnailLink null
thumbnailVersion null
trashed null
trashedTime null
version null
viewedByMe null
viewedByMeTime null
viewersCanCopyContent null
webContentLink "https://drive.google.com/uc?id=1gYCx2d_pmAgfPNQSRxZcSnv1O0cfGied&export=download"
webViewLink "https://drive.google.com/file/d/1gYCx2d_pmAgfPNQSRxZcSnv1O0cfGied/view?usp=drivesdk"
writersCanShare null
答案 2 :(得分:0)
更具体一点,$service->files->get()
调用可以采用第二个参数,其中包含您要返回的字段列表。例如只检查可以使用的文件或文件夹的废纸flag:
$optParams = array('fields' => 'trashed');
$filedata = $service->files->get($file->getId(),$optParams);
if ($filedata->getTrashed()) {
// The file or folder was trashed
}