jadent
jadent

Reputation: 3904

Get all Drive files across all users

I have a Service Account setup with Domain Wide Delegation. I'd like to search files across all users with a single call.

I'm using the Drive API and the OAuth2 project in Google Apps Script to handle the authentication. It seems like you always have to specify a user to impersonate (code below) when making calls. When I remove the setSubject method I get no results.

Is there a way to do this?

  OAuth2.createService('GoogleDrive:' + userEmail)
  // Set the endpoint URL.
  .setTokenUrl('https://accounts.google.com/o/oauth2/token')

  // Set the private key and issuer.
  .setPrivateKey(PRIVATE_KEY)
  .setIssuer(CLIENT_EMAIL)

  // Set the name of the user to impersonate. This will only work for
  // Google Apps for Work/EDU accounts whose admin has setup domain-wide
  // delegation:
  // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
  .setSubject(userEmail)

  // Set the property store where authorized tokens should be persisted.
  .setPropertyStore(PropertiesService.getScriptProperties())

  // Set the scope. This must match one of the scopes configured during the
  // setup of domain-wide delegation.
  .setScope('https://www.googleapis.com/auth/drive');

Thanks

Upvotes: 0

Views: 1062

Answers (1)

Tesseract
Tesseract

Reputation: 8139

You could try the Drive API which can be accessed from apps script through the Advanced Drive Service. It has additional features compared to DriveApp and allows you to use search queries. You can also use the API Explorer to test your requests. For example copy the query string from the Advanced Drive Service page into the q field of the API Explorer. That query string is "root" in parents and trashed = false and mimeType = "application/vnd.google-apps.folder". If you then click on execute you will see the result. The syntax for the query string is explained here.

Upvotes: 0

Related Questions