簡體   English   中英

Google Drive API PHP:Files.get返回空值

[英]Google Drive API PHP: Files.get returns null value

我正在創建一個與Google Drive API(版本3)集成的應用程序,以便它可以通過用戶輸入在命名文件夾中查找文件,然后獲取該文件的網絡內容鏈接。 我的應用程序正在與一個服務帳戶聯系,該帳戶將存儲我的應用程序的文件並獲取鏈接。 唯一的問題是,當我使用下面顯示的代碼時,它返回下面顯示的輸出,除了一些字段外,所有輸出均為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方法將返回什么,此刻目前為空,因此現在不期望有太多功能。 下面的代碼不應該下載任何東西,僅僅是調試。 因此,我需要知道的是,如何訪問此應用程序從API找到的3個文件的webContentLink屬性?

閱讀此Drive API V3遷移文檔。

默認情況下,不再返回完整資源。 使用fields查詢參數可以請求返回特定的字段。 如果未指定,則僅返回常用字段的子集。

我知道已經晚了,但我希望我能幫助別人。 我正在使用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

只是更具體一點, $service->files->get()調用可以采用第二個參數,其中包含您要返回的字段列表。 例如,僅檢查文件或文件夾的廢紙flag,您可以使用:

$optParams = array('fields' => 'trashed');
$filedata = $service->files->get($file->getId(),$optParams);

if ($filedata->getTrashed()) {
  // The file or folder was trashed
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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