Reputation: 2479
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"
]
}
Upvotes: 1
Views: 53
Reputation: 38391
Consider validating that the user has access to the file. If they don't, exit elegantly rather than throwing an error.
It's known that Google Apps Script Html Service, used to create dialogs and sidebars, cannot handle a user signed in to multiple Google accounts.
You could include this limitation in your add-on documentation for the end-user and as feedback to the user when calling the referred function from your add-on's sidebar.
Related
Upvotes: -1