Home > Enterprise >  Previously working Google App Script now produces permission error
Previously working Google App Script now produces permission error

Time:04-07

I cannot find anything about Google having updated policies on April 2022. This script was working without any issues before, then in the middle of the day, permissions needed to be re-added. Even with said permissions granted, Google still says access is denied. It was later discovered that the macro has not ran successfully since March 31st.

  var newJobFile = DriveApp.getFileById(newJobID);
  newJobFile.AddEditors(['[email protected]','[email protected]');
  newJobFile.setOwner('[email protected]');

The last line is what generates the error. When debugging, I can see as the variable gets created, I cannot see any information about it. For example, if I add a "Filecreated.getName();" before the line with the error, the variable menu remains blank. I am not sure if this is normal. The "newid" variable is confirmed to have the spreadsheet ID. I can copy it into the URL and it takes me to the page. Why is this suddenly an issue and how can I fix it? I did not have a appscripts.json file before with OAuthScopes and it worked fine then. I've added it with the proper permissions, but it does not change anything. I've added several permission scopes to try to resolve this but none of them do. Any advice?

"oauthScopes": [      
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/spreadsheets"
]

EDIT: I am still having this issue. Even with everything controlled under one account, I cannot transfer ownership to '[email protected]' due to 'Exception : Access Denied : DriveApp'. I have found just one other person experiencing this same issue on the Google App Script group page. Is anyone else not having issues with transferring ownership starting in April on a regular, non-work account?

EDIT 2: Sorry for not uploading my code earlier, I had to clean it up first.

function onOpen(e) {
  SpreadsheetApp.getUi()
    .createMenu('Add')    //creates toolbar entry to the right of 'Help'
      .addItem('Capital project','newProject')
      .addToUi();
}

function newProject(){
  var dashboard = SpreadsheetApp.getActiveSpreadsheet();    //Stores dashboard into a variable for later
  var template = SpreadsheetApp.openById('xxxxxx')    //Opens the project sheet template in the backend
  var nameEntry = getName();                                //Prompts user for the name of the new job file
  if (nameEntry == null){
    return;
  }

  SpreadsheetApp.setActiveSpreadsheet(template);            //Template is now the active sheet

  var newJobID = copySheet(nameEntry);                    //Creates a copy of the New Project template
  var newJobFile = DriveApp.getFileById(newJobID);

  newJobFile.AddEditors(['[email protected]','[email protected]');
  newJobFile.setOwner('[email protected]');
}

function getName(){
  var ui = SpreadsheetApp.getUi();
  var name = ui.prompt(                                      //Prompts the user for an input name      
    '',
    '?????-? Project Description',
    ui.ButtonSet.OK_CANCEL);
  var cancelCheck = name.getSelectedButton();
  if (cancelCheck == ui.Button.CANCEL || cancelCheck == ui.Button.CLOSE) {
    return null;
  } 
  var sheetName = name.getResponseText();
  return sheetName;
}

function copySheet(name) {
  var activeSS = SpreadsheetApp.getActive();
  var newFile = activeSS.copy(name)                       //Creates a copy of the New Project template

  SpreadsheetApp.setActiveSpreadsheet(newFile);              //Resets active spreadsheet to the recent copy
  activeSS = SpreadsheetApp.getActive();

  activeSS.getRange('A1').activateAsCurrentCell();
  activeSS.getCurrentCell().setValue(name);                  //Set cell A1 to the name of the file

  var newHyperlink = '=HYPERLINK("'   activeSS.getUrl()   '#gid=15580246",A1)';
  activeSS.getRange('A2').activateAsCurrentCell();
  activeSS.getCurrentCell().setValue(newHyperlink);

  return activeSS.getId();
}

With logs, I can see that everything works as intended except for the .setOwner() method. It returns the Access Denied error. I have checked all google accounts and each has enabled CustomScripts to access their drive. Has .setOwner() been deprecated for non workspace accounts?

I've updated my original snippets to match my code.

CodePudding user response:

I don't have enough reputation to comment, but I'd second what Lorena Gomez said that it's likely an issue with your OAuthScopes. According to the enter image description here

And I found this enter image description here

I tested the setOwner() method with a Google Workspace account and it works as within Google Workspace you can directly transfer file ownership between users within the same organization, then I tested the same script with a Gmail account and tried to set another Gmail account as the new owner and I got the same error message: "Exception: Access denied: DriveApp".

Based on all the information, it seems that this behavior is expected as the process to change a file ownership for Gmail accounts is different now, you can’t set a new owner directly, the person you invite to own the file must accept your request to complete the transfer.

  • Related