【问题标题】:Selecting specific fields Google Drive API v3选择特定字段 Google Drive API v3
【发布时间】:2019-05-23 08:41:10
【问题描述】:

我终于让 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);

所以我特别要求文件数组中的某些字段。问题是,所有其他字段仍然存在(除了它们都是 null 值)。这是正常的行为吗?因为如果我尝试检索 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 ▶}

【问题讨论】:

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


    【解决方案1】:

    google drive api v3 实现了一个名为 Partial response 的东西,实际上大多数 google api 都有这个 fields 是一个可选参数。

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

    它的 IMO 并没有很好地记录,因为上述陈述是正确的。

    Drive v3 默认情况下会发回完整的表示。这是与 drive 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' => '*'
    ];
    

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

    【讨论】:

    • 嗯,我使用github.com/googleapis/google-api-php-client作为API...谷歌官方API不应该考虑字段选项吗?
    • 客户端库是自动生成的。 PHP中解析对象的方式意味着如果类中包含一个字段但没有填写,那么它将显示为null。
    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多