google-apps-scriptgoogle-apigoogle-drive-apigoogle-oauth

Not able to export Google Drive files using drive.file scope


As mentioned in Drive V3 - files.export API documentation and API scopes documentation, if I have access to drive.file scope I should be able to export the drive dcoument, but it is not working as expected.

I have a Google Docs Add-on open in a document and I am trying to export current document to .docx format using following code. Note that I have access to https://www.googleapis.com/auth/drive.file scope.

function getDocxBlob() {
  // Get the ID of the active document
  var doc = DocumentApp.getActiveDocument();
  var fileId = doc.getId();

  // Construct the Drive API export URL for DOCX
  var url = 'https://www.googleapis.com/drive/v3/files/' + fileId + 
            '/export?mimeType=application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  
  // Set up the request options with OAuth authentication
  var options = {
    'method': 'GET',
    'headers': {
      'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
    },
    'muteHttpExceptions': false // Prevents the script from throwing an exception on non-200 responses
  };

  // Make the API request
  var response = UrlFetchApp.fetch(url, options);

  // Check if the request was successful
  if (response.getResponseCode() == 200) {
    var docxBlob = response.getBlob();
    return docxBlob; // Return the DOCX blob
  } else {
    // Handle errors by throwing a descriptive exception
    throw new Error('Failed to export document: ' + response.getContentText());
  }
}

But I get following error -

Error: Failed to export document: {
  "error": {
    "code": 404,
    "message": "File not found: 1q3uEP9VruS3SQC2Q--79LPAzOTqKVhkE(modified docId)",
    "errors": [
      {
        "message": "File not found: 1q3uEP9VruS3SQC2Q--79LPAzOTqKVhkE(modified docId)",
        "domain": "global",
        "reason": "notFound",
        "location": "fileId",
        "locationType": "parameter"
      }
    ]
  }
}

It works when I use https://www.googleapis.com/auth/drive.readonly scope. Can someone help me understand whey https://www.googleapis.com/auth/drive.file scope is not working as expected?

My appscript.json

{
  "timeZone": "Asia/Kolkata",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "Drive",
        "version": "v3",
        "serviceId": "drive"
      }
    ]
  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/drive.file",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/script.container.ui",
    "https://www.googleapis.com/auth/documents",
    "https://www.googleapis.com/auth/script.scriptapp",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile",
    "openid"
  ]
}

Solution

  • Feature Not Yet Available

    There is an active feature request for this issue, and it is currently assigned and posted on:

    Drive export API does not work with OAuth 2.0 scope 'drive.file'

    You may click on +1 to let them know that you also want this feature to be implemented.


    As of today, the latest update is a comment from the reporter:

    Calling export API (files.export resource) with drive.file scope always results in 404 error, saying that the document whose ID passed as fileId parameter was not found