How can I write a Google App Script that downloads a file from the Google Drive to my computer? - Stack Overflow

admin2025-04-17  4

I found a prototype of such a script at Google itself: .js. However, when I try to run it, I keep getting the same error: "ReferenceError: require is not defined" It seems to be referring in particular to the following line: const {GoogleAuth} = require('google-auth-library');

Here's the whole script:


async function downloadFile(realFileId) {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({
    scopes: '',
  });
  const service = google.drive({version: 'v3', auth});

  fileId = realFileId;
  try {
    const file = await service.files.get({
      fileId: fileId,
      alt: 'media',
    });
    console.log(file.status);
    return file.status;
  } catch (err) {
    // TODO(developer) - Handle error
    throw err;
  }
}

I tried researching and mucking around with the GoogleAuth, but I obviously don't understand it well enough to make any headway. I'm not even sure the problem is the script itself. For example: Is there some permission setting somewhere on the Google Drive that can prevent this script from running?

转载请注明原文地址:http://anycun.com/QandA/1744862299a88669.html