【问题标题】:Google Drive API advanced file searchGoogle Drive API 高级文件搜索
【发布时间】:2017-07-05 14:48:54
【问题描述】:

我正在尝试开发一个 PHP 脚本,该脚本通过我的 Google Drive 列出我的文件 .我有当前的结构:

 -- logo1.png
|
|-- logo2.png
|
 -- myFolder
|     |
|      --logo3.png
|
|
 -- myPrivateFolder
      |
       --logo4.png

我正在使用 Google PHP 库进行查询。要列出我的文件,我有以下代码:

$client = new Google_Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);


$result = array();

try {
  $parameters = array(
      'q' => "mimeType contains 'image/'",
  );
  $files = $service->files->listFiles($parameters);

  $result = array_merge($result, $files->getFiles());
} catch (Exception $e) {
  print "Une erreur s'est produite : " . $e->getMessage();
}
return $result;

$result 是一个包含 4 个元素的数组。因为我指定我只想要 MIME 类型“图像/”,所以我只得到了 4 张图像。现在,我想知道,如何过滤我的结果以从研究中排除我的私人目录?我希望能够指定不查看哪个文件夹?

我无法在文档中找到任何相关内容。提前感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    首先,您需要获取私有目录的 ID。你可以通过查询来做到这一点

    name = 'my_private_directory'
    

    获得 ID 后,您可以使用

    扩展现有查询

    and not 'xxxxxx' in parents 其中 xxxx 是步骤 1 中的文件 ID。

    【讨论】:

    • 感谢您的回答!工作很棒。如果我有更多文件夹,我该如何调整我的请求? mimeType contains 'image/' and not 'X1' in parents and not 'X2' in parents and not X3' in parents and not 'X4' in parents
    • 另外,我只想获取我的文件,而不是 sharedWithMe 文件。因此,我添加了and sharedWithMe = false,但出现了这个错误:{ "error": { "errors": [ { "domain": "global", "reason": "invalid", "message": "Invalid Value", "locationType": "parameter", "location": "q" } ], "code": 400, "message": "Invalid Value" } }
    • mimeType contains 'image/' and not 'X1' in parents and not 'X2' in parents and not X3' in parents and not 'X4' in parents and sharedWithMe = false 但如果我写 = true 它可以工作,但我希望它设置为 false...
    • 看起来像是 Drive API 中的一个错误
    • 好的,非常感谢您的帮助。你知道我是否可以将我的脚本应用到多个帐户?我的意思是不是在我的浏览器中打开我的 PHP 页面来进行 ListFiles 搜索,我可以有一个服务器端 php 脚本来列出多个谷歌帐户的文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2014-11-07
    • 1970-01-01
    相关资源
    最近更新 更多