选择特定字段Google Drive API v3

时间:2019-05-23 08:41:11

标签: laravel google-api google-drive-api google-api-php-client

我终于有了Google Drive API V3,可以与服务帐户一起使用。

现在要从驱动器检索所有文件,我使用以下命令:

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

因此,我专门要求在files数组中输入某些字段。问题在于,所有其他字段仍然存在(除了它们均为值)。这是正常行为吗?原因是,如果我尝试检索20到50个文件,但仍在传输一些无用的Kb。

响应示例:

  +"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 ▶}

1 个答案:

答案 0 :(得分:1)

google drive api v3隐含了一个称为Partial response的东西,实际上大多数google api都具有此fields是可选的参数。

  

默认情况下,服务器在处理请求后发回资源的完整表示。为了获得更好的性能,您可以要求服务器仅发送您真正需要的字段,并获得部分响应。

由于上面的陈述是正确的,因此它的IMO并不是每一个都有据可查的文件。

Drive v3默认情况下发送回完整的表示形式。这是与驱动器v3的主要区别,其他api通常通常是默认值,即返回所有内容,并且仅在开发人员使用字段parm进行请求时才执行部分响应。

驱动器files.list响应包含一个文件列表,只有默认情况下,它才会实际将以下4个字段返回给您。

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

您看到的空值实际上可能来自您正在将空对象值解析为空值的库中。

如果您进行

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

它实际上将为您填写所有字段。