Google 驱动器 API 上传带有自定义 属性 的文件,响应中缺少属性

Google Drive API upload file with custom property, properties missing from response

我正在尝试保存具有自定义属性的 google 驱动器文件,但它不起作用,并且由于 Google API 的文档非常糟糕,我正在努力这个。到目前为止,这是我的代码:

$g = new FileCtrl();
$fileMeta = new Google_Service_Drive_DriveFile();
$fileMeta->name = $file['name'];
$fileMeta->parents = [$g->googleDriveFolderId];
$fileMeta->appProperties = (object)['lead_id'=>$file['lid'], 'sales_package_id'=>$file['pid'], 'user_id'=>$user->uid];
//$fileMeta->size = $file['size'];

$fileData = array(
              'data' => $fileContent,
              'mimeType' => $file['media_type'],
              'uploadType' => $file['multipart'],
            );

 //create file in google drive
$res = $g->client->files->create($fileMeta, $fileData);

它没有返回错误并且事件已保存,但没有自定义属性!?

我刚刚测试了这个。

要求:

{
  "name": "Linda Test",
  "mimeType": "application/vnd.google-apps.document",
  "appProperties": {
    "TestingId": "1"
  }
}

响应:

{
 "kind": "drive#file",
 "id": "1mhP2EW4Kbl81F5AuJ4zJ2IPoeI56i_Vd5K-dfGJRj6Y",
 "name": "Linda Test",
 "mimeType": "application/vnd.google-apps.document"
}

执行 file.get 检查文件元数据。

{
 "kind": "drive#file",
 "id": "1mhP2EW4Kbl81F5AuJ4zJ2IPoeI56i_Vd5K-dfGJRj6Y",
 "name": "Linda Test",
 "mimeType": "application/vnd.google-apps.document",
 "starred": false,
 "trashed": false,
 "explicitlyTrashed": false,
 "parents": [
  "0AJpJkOVaKccEUk9PVA"
 ],
 "appProperties": {
  "TestingId": "1"
 },
  ...
}

我建议您使用 File.get method after its returned to be sure that it was added. To my knowledge there is now way to see these added fields via the Google drive website interface. If after ruining a file.get you still find that it wasn't uploaded i suggest you log this as a bug with the Google APIs PHP client 图书馆小组检查您上传的文件。这适用于原始休息请求,它可能是图书馆的问题。

您可能正在查找返回的文件资源中的属性。

它们不存在的原因是 Drive 仅 returns 少量文件属性(名称、mime 类型、id)。如果您想查看完整的文件资源,您需要在请求中包含 fields=*。所以正确的请求看起来像

POST https://content.googleapis.com/drive/v3/files?fields=*

我不知道 PHP 库,所以你需要弄清楚这一点。它会像

'fields' => '*',