Failing to get a file in Google Apps Script using the Drive API

Viewed 689

I have a google apps script (that I use in one of my spreadsheets). I'm used to calling the DriveApp service to handle files (list, create, move, etc), but I wanted to get the user that last modified a file. Looking it up in several places, I understood that the only way to achieve that is to import the Drive API SDK (the Advanced Drive Service) into the Google Apps script. I added it using the official documentation. So, here is where I'm stuck. To simplify, look at the code below:

console.log(DriveApp.getFileById('1eMIUTEmpvzN5a3kK6PWbK4W8RCkbNOSxxxxxxxxxxx').getName())
console.log(Drive.Files.get('1eMIUTEmpvzN5a3kK6PWbK4W8RCkbNOSxxxxxxxxxxx').title)

The first one shows my file name just fine. The second one fails with:

GoogleJsonResponseException: API call to drive.files.get failed with error: File not found: 1eMIUTEmpvzN5a3kK6PWbK4W8RCkbNOSxxxxxxxxxxx

(anonyme) @ List Files.gs:2

Any ideas why it wouldn't work?

1 Answers

From your script and your error of File not found, I thought that in your situation, the file might be put in the shared Drive. If my understanding is correct, such error occurs. On the other hand, when DriveApp.getFileById is used, the file in the shared Drive can be retrieved. So in your situation, how about the following modification?

From:

console.log(Drive.Files.get('1eMIUTEmpvzN5a3kK6PWbK4W8RCkbNOSxxxxxxxxxxx').title)

To:

console.log(Drive.Files.get('1eMIUTEmpvzN5a3kK6PWbK4W8RCkbNOSxxxxxxxxxxx', {supportsAllDrives: true}).title)

Reference:

Related