C02
C02

Reputation: 155

How to filter Google Drive API v3 mimeType?

I wrote a script that uses DriveApp to read Google Drive, SpreadsheetApp to log data in Sheets, and Google Drive API v3 + service account + OAuth to make changes on behalf of G Suite users.

It would be nice to search Google Drive from the target user perspective (calling Google Drive API v3) instead of the account running the script (calling DriveApp). I can't get the filter to work.

The query is built with parent folder keys, mimeType = or mimeType != for folders, and is passed into the function. The format is:

var query = "('GoogleDriveFolderKey01' in parents or 'GoogleDriveFolderKey02' in parents) and trashed = false and mimeType = 'application/vnd.google-apps.folder'"

The DriveApp function uses:

files = Drive.Files.list({
  q: query,
  maxResults: 100,
  pageToken: pageToken
});

The Google Drive API v3 function uses:

var url = 'https://www.googleapis.com/drive/v3/files/'
var options = {
  'contentType': 'application/json',
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
  'corpora'    : 'domain',
  'q'          : query,
  'spaces'     : 'drive',
  'pageSize'   : 100,
  'pageToken'  : pageToken
};
var response = UrlFetchApp.fetch(url, options);

var resultParsed = JSON.parse(response.getContentText());
files = resultParsed.files
pageToken = resultParsed.pageToken

The results with DriveApp are as expected, but Google Drive API v3 results in:

"files": [
{
  "kind": "drive#file",
  "id": "01abc123_etc",
  "name": "Something something (2021-04-15).pdf",
  "mimeType": "application/pdf"
},
{
  "kind": "drive#file",
  "id": "02ABC4321-qwertyuiop",
  "name": "Super Special Worksheet",
  "mimeType": "application/vnd.google-apps.spreadsheet"
},
{
  "kind": "drive#file",
  "id": "whatever",
  "name": "Copy of V1",
  "mimeType": "application/vnd.google-apps.folder"
},
...

Any ideas?

Edit:

Thank you! The problem seems to be 'corpus' and 'space'. I have used to following in Google AppsScript:

var options = {
  'contentType': 'application/json',
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
  'pageSize'   : 100
};
url += '?q=' + encodeURIComponent(query);
if ( pageToken.length > 0 ) url += '&pageToken=' + pageToken;
var response = UrlFetchApp.fetch(url, options);

Upvotes: 1

Views: 2806

Answers (1)

Tanaike
Tanaike

Reputation: 201553

How about this answer?

Modification points:

  • At the method of "Files: list" in Drive API v3, the values of corpora, q, space, pageSize, pageToken are used as the query parameters. I think that the reason of your issue is due to this.
  • At GET method, contentType is not required.

Modified script:

When your script is modified, it becomes as follows.

var query = "('GoogleDriveFolderKey01' in parents or 'GoogleDriveFolderKey02' in parents) and trashed = false and mimeType = 'application/vnd.google-apps.folder'"

var url = 'https://www.googleapis.com/drive/v3/files'  // Modified
var options = {  // Modified
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
};
url += `?corpora=domain&q=${encodeURIComponent(query)}&spaces=drive&pageSize=100&pageToken=${pageToken}`;  // Added

var response = UrlFetchApp.fetch(url, options);
var resultParsed = JSON.parse(response.getContentText());
files = resultParsed.files
pageToken = resultParsed.pageToken

Note:

  • This modified script supposes that service.getAccessToken() can be used for using the method of "Files: list" in Drive API v3.
  • If an error occurs please remove corpora=domain from the URL like below and test it again.

    url += `?q=${encodeURIComponent(query)}&spaces=drive&pageSize=100&pageToken=${pageToken}`;
    

Reference:

Upvotes: 1

Related Questions