2016-10-10 60 views
0

我正在創建一個與Google Drive API(版本3)集成的應用程序,以便通過用戶輸入的方式在命名文件夾中查找文件,然後獲取該文件的網頁內容鏈接。我的應用程序正在聯繫我的應用程序文件將被存儲並獲取鏈接的服務帳戶。唯一的問題是,當我使用下面顯示的代碼時,它會返回下面顯示的輸出,除了幾個字段外,它都是NULL。我究竟做錯了什麼?Google Drive API PHP:Files.get返回空值

代碼導致該問題:

<?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) { 
    } 
} 

現在它只是一堆的測試代碼,看看是什麼文件獲取方法將要返回,目前這是很多空,所以現在不要指望太多的功能。下面的代碼不應該下載任何東西,它只是調試。所以,我需要知道的是,如何訪問此應用程序從API中找到的3個文件的webContentLink屬性?

回答

1

請將此Drive API V3移植文檔中的read

默認情況下,不再返回全部資源。使用fields查詢參數來請求返回特定的字段。如果未指定,只返回常用字段的一個子集。

+0

那麼,如何將此字段屬性添加到Files.get方法? –

+0

請參考PHP快速入門頁面:https://developers.google.com/drive/v3/web/quickstart/php –

+0

好的。謝謝。今晚我會試試看,我相信它會奏效。如果是這樣,我會將其標記爲答案。謝謝。如果有效,我會將其設置爲答案 –