Goowik
Goowik

Reputation: 803

Selecting specific fields Google Drive API v3

I finally got the Google Drive API V3 to work with a service account.

Now to retrieve all files from a drive I use the following:

$optParams = [
    'corpora' => 'drive',
    'driveId' => env('GOOGLE_DRIVE_ID'),
    'includeItemsFromAllDrives' => true,
    'supportsAllDrives' => true,
    'fields' => 'files(name,mimeType,trashed,parents,version,webContentLink,webViewLink,createdTime,modifiedTime,size)'
];

$this->googleDrive->files->listFiles($optParams);

So I specifically ask for certain fields in the files array. The issue is, that all other fields are still there (except that they're all of null value). Is that a normal behaviour? Cause if I'm trying to retrieve 20 to 50 files that's still a few useless Kb's being transferred.

Example of a response:

  +"files": array:2 [▼
    0 => Google_Service_Drive_DriveFile {#279 ▼
      #collection_key: "spaces"
      +appProperties: null
      #capabilitiesType: "Google_Service_Drive_DriveFileCapabilities"
      #capabilitiesDataType: ""
      #contentHintsType: "Google_Service_Drive_DriveFileContentHints"
      #contentHintsDataType: ""
      +copyRequiresWriterPermission: null
      +createdTime: "2019-05-22T11:41:25.852Z"
      +description: null
      +driveId: null
      +explicitlyTrashed: null
      +exportLinks: null
      +fileExtension: null
      +folderColorRgb: null
      +fullFileExtension: null
      +hasAugmentedPermissions: null
      +hasThumbnail: null
      +headRevisionId: null
      +iconLink: null
      +id: null
      #imageMediaMetadataType: "Google_Service_Drive_DriveFileImageMediaMetadata"
      #imageMediaMetadataDataType: ""
      +isAppAuthorized: null
      +kind: null
      #lastModifyingUserType: "Google_Service_Drive_User"
      #lastModifyingUserDataType: ""
      +md5Checksum: null
      +mimeType: "application/zip"
      +modifiedByMe: null
      +modifiedByMeTime: null
      +modifiedTime: "2019-05-22T11:41:25.852Z"
      +name: "<something>"
      +originalFilename: null
      +ownedByMe: null
      #ownersType: "Google_Service_Drive_User"
      #ownersDataType: "array"
      +parents: array:1 [▶]
      +permissionIds: null
      #permissionsType: "Google_Service_Drive_Permission"
      #permissionsDataType: "array"
      +properties: null
      +quotaBytesUsed: null
      +shared: null
      +sharedWithMeTime: null
      #sharingUserType: "Google_Service_Drive_User"
      #sharingUserDataType: ""
      +size: "455778"
      +spaces: null
      +starred: null
      +teamDriveId: null
      +thumbnailLink: null
      +thumbnailVersion: null
      +trashed: false
      +trashedTime: null
      #trashingUserType: "Google_Service_Drive_User"
      #trashingUserDataType: ""
      +version: "2"
      #videoMediaMetadataType: "Google_Service_Drive_DriveFileVideoMediaMetadata"
      #videoMediaMetadataDataType: ""
      +viewedByMe: null
      +viewedByMeTime: null
      +viewersCanCopyContent: null
      +webContentLink: "<something>"
      +webViewLink: "<something>"
      +writersCanShare: null
      #internal_gapi_mappings: []
      #modelData: []
      #processed: []
    }
    1 => Google_Service_Drive_DriveFile {#269 ▶}

Upvotes: 1

Views: 3208

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117166

The google drive api v3 implemented something called Partial response actually most google apis have this fields is a optional parm.

By default, the server sends back the full representation of a resource after processing requests. For better performance, you can ask the server to send only the fields you really need and get a partial response instead.

Its IMO not every well documented because the above statement is in correct.

Drive v3 does NOT by default send back a full representation. This is the main difference with drive v3 and the other apis is normally the default is to return everything and only do a partial response if the developer requests it using the fields parm.

A drive files.list response contains a list of files and it only actually by default will return the following 4 fields to you.

{
   "kind": "drive#file",
   "id": "hzqXfMiOiFlrYdQCx3Rram0vuf9lmXa",
   "name": "Sayak",
   "mimeType": "application/vnd.google-apps.folder"
  }

The nulls you are seeing are in fact probably coming from the library that you are using parsing the empty object values as null.

if you do a

$optParams = [
    'corpora' => 'drive',
    'driveId' => env('GOOGLE_DRIVE_ID'),
    'includeItemsFromAllDrives' => true,
    'supportsAllDrives' => true,
    'fields' => '*'
];

It will in fact fill out all the fields for you.

Upvotes: 1

Related Questions