0

I am trying to query the google drive using multiple folder ids.

This is my query

 $optParams = array(
 'pageSize' => 1000
    ,'fields' => 'nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents, kind, trashed, owners, modifiedTime)'
    ,[q] => "trashed=false and ('folder-id-1' in parents or 'folder-id-2' in parents or 'folder-id-3' in parents or 'folder-id-4' in parents or 'folder-id-5' in parents)"
   );                

 $service = new \Google_Service_Drive($this->GoogleClient);
 $results = $service->files->listFiles($optParams);

But it is only returning files from the first 2 folders


Edit with more details about my query and the results returned. Folder Ids have been anominised.

    $fldrs = [];
    $fldrs[] = "folder-id-1"; // Toolkit 3 folders (Shared with me)
    $fldrs[] = "folder-id-2"; // Untitled folder 1 file (mine)
    $fldrs[] = "folder-id-3"; // Logos 14 files (not mine)
    $fldrs[] = "folder-id-4"; // Posters (8 files - not mine)
    $fldrs[] = "folder-id-5"; // Promo Assets (11 files not mine)

    $q = "trashed=false and (";
    $MaxFolders = 5;
    for ($i=0;$i<$MaxFolders;$i++)
    {
        if ($i > 0) $q .= " or ";
        $q .= "'{$fldrs[$i]}' in parents";
    }

    $q .= ")";

    $optParams = array(
         'pageSize' => 1000
        ,'fields' => 'nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents, kind, trashed, owners, modifiedTime, driveId)'
        ,q => $q
      );                

    $service = new \Google_Service_Drive($this->GoogleClient);
    $results = $service->files->listFiles($optParams);

    ------

The $optParams (from debug)

    array(3) (
      [pageSize] => (int) 1000
      [fields] => (string) nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents, kind, trashed, owners, modifiedTime, driveId)
      [q] => (string) trashed=false and ('folder-id-1' in parents or 'folder-id-2' in parents or 'folder-id-3' in parents or 'folder-id-4' in parents or 'folder-id-5' in parents)
    )

This only returns 5 files (folders 3 and 4, and 3 files : 1 file from folder 2, 1 file from folder 3, 1 file from folder 5)

If I work through and just do the 1st folder, I get 3 folders returned. If I do 2 folders, I get 2 folders and 1 file If I do 3 folders, I get 2 folders and 2 files If I fo 4 folders, I get 2 folders and 2 files (same as doing 3 folders)

If I pick to do just folder 3 (logos) I get all 14 files.

Most files are not my own. they are shared with me.

StripyTiger
  • 877
  • 1
  • 14
  • 31
  • I think that your search query is correct. So can I ask you about the reason that you use `[q]` instead of `'q'`? For example, when you modified `,[q] => "trashed=false,,,` to `,'q' => "trashed=false,,,`, what result will you get? In my environment, when I used `,[q] =>`, I could confirm that the file list I don't expect is returned. I'm not sure whether this is the direct solution of your issue. So I posted this as a comment. – Tanaike Apr 20 '20 at 01:03
  • Thanks - I think this was because was piecing it together for the question. In reality I am adding to the q parameter further down the script like this $optParama['q'] = and ('folder-id-1' in parents or..... I am trying it now as you say, and initial tests make no difference. I will confirm this either way shortly. – StripyTiger Apr 20 '20 at 08:40
  • Thank you for replying. When I tested your search query, I could confirm that it worked. So if the same issue occurs even when you tested the modification, can you provide the whole script? By this, I would like to check it. – Tanaike Apr 20 '20 at 08:44
  • Thank you - I have added more information into the question as it wouldn't fit in the comment, and also formatting. I should note that lots of the files are shared with me, and not my own. But if I just pick a single folder using the same query, all files are returned regardless of ownership – StripyTiger Apr 20 '20 at 10:00
  • Thank you for replying. When I tested your additional script, I could confirm that the correct number of files can be retrieved from the sample 5 folders. So for example, can I ask you about the scopes you are using? If you are using `https://www.googleapis.com/auth/drive.file`, for example, how about using `https://www.googleapis.com/auth/drive.metadata.readonly` or `https://www.googleapis.com/auth/drive.readonly` or `https://www.googleapis.com/auth/drive`? – Tanaike Apr 20 '20 at 12:07
  • Thanks again Tanaike - Currently, I am using metadata.readonly. I will try with another.This is my code. $this->GoogleClient = new \Google_Client(); $credentialsPath = $PluginPath . '/config/GoogleClientSecret.json'; $this->GoogleClient->setAuthConfig($credentialsPath); $this->GoogleClient->addScope(\Google_Service_Drive::DRIVE_METADATA_READONLY); $this->GoogleClient->setAccessType('offline'); – StripyTiger Apr 20 '20 at 12:15
  • I think that your script and the search query is correct. And also, I think that your scope can be used. So, as a test case, when 5 folders that the owner is you and 1 file, that you are the owner, is put in each folder, when your script is run for the folders, what result will you get? If 5 files are retrieved, it is found that the shared files and folders might be related to your issue. How about this? – Tanaike Apr 20 '20 at 12:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212082/discussion-between-stripytiger-and-tanaike). – StripyTiger Apr 20 '20 at 12:41

1 Answers1

1

The parameter isnt an array. Try this.

 $optParams = array(
 'pageSize' => 1000
    ,'fields' => 'nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents, kind, trashed, owners, modifiedTime)'
    ,'q' => "trashed=false and ('folder-id-1' in parents or 'folder-id-2' in parents or 'folder-id-3' in parents or 'folder-id-4' in parents or 'folder-id-5' in parents)"
   );  
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449