Reputation: 67259
I have a script that first presents a UI containing just a button to the user. Upon clicking that button, the script goes through their documents and changes the ownership of some of the files.
Upon loading my script, the user is greeted with the standard "Authorization Required" page with the red border. The user clicks the button to grant my app the needed permissions, and is taken to the page with the button.
However, after clicking the button, the user gets a dialog that says:
Error Encountered: Authorization is required to perform that action.
The app is set to execute as the user executing the script, and the script access is set to "anyone."
Any thoughts on what might be wrong?
My doGet():
function doGet(e) {
var app = UiApp.createApplication();
var button = app.createButton('Transfer');
app.add(button);
var handler = app.createServerHandler('init');
button.addClickHandler(handler);
return app;
}
The init() method goes through a specific folder (statically set by it's ID in the script), looks for files owned by the current user, and transfers the ownership to another user.
function init() {
// Create new log as a a Drive document
var log = DocsList.createFile("Ownership Transfer Log");
var user = Session.getActiveUser();
var targetEmail = "[email protected]";
transferFolderOwnership('[Drive folder ID]', user.getEmail(), targetEmail, log);
}
And transferFolderOwnership:
function transferFolderOwnership(folderId, userEmail, targetEmail, log) {
var rootFolder = DocsList.getFolderById(folderId);
var files = rootFolder.getFiles();
// Transfer files
var file;
for (var i = 0; i < files.length; i++) {
file = DriveApp.getFileById(files[i].getId());
if (file.getOwner() == userEmail) {
file.transferOwnership(targetEmail);
}
}
// Do the same for folders
}
Upvotes: 1
Views: 441
Reputation: 46812
The library you are using need an authorization that can only be granted when called from the script editor (see end of the library source code : function googleOAuth_()
) . The first authorization you see is only for the "local" part of you script, the other looks like this :
This is a known issue and has a possible workaround described in one of the last issue tracker posts. I didn't try it yet but I guess it could be a way to go...
Upvotes: 1