I wish to authorize my webapp to create a folder in the user's appfolder to hold the app's data files.
To do this, I need to request the scope https://www.googleapis.com/auth/drive.appfolder
So far I have the following code:
var CLIENT_ID = '3941...';
var CLIENT_SECRET = 'DY_P...';
var SCRIPT_ID = '1XAF...';
var appfolder_scope = 'https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.appfolder';
var redirectURI = 'https%3A%2F%2Fscript.google.com%2Fmacros%2Fd%2F'+ SCRIPT_ID + '%2Fusercallback';
var AuthEndpoint = 'https://accounts.google.com/o/oauth2/v2/auth';
function getCallbackURL(callbackFunction) {
var url = ScriptApp.getService().getUrl(); // Ends in /exec (for a web app)
url = url.slice(0, -4) + 'usercallback?state='; // Change /exec to /usercallback
var stateToken = ScriptApp.newStateToken()
.withMethod(callbackFunction)
.withTimeout(120)
.createToken();
return url + stateToken;
}
function generateAuthRequestURL() {
var AuthRequest = AuthEndpoint;
var Query = '?'
+ 'scope=profile%20' + appfolder_scope
+ '&state=' + getCallbackURL(cb)
+ '&redirect_uri=' + redirectURI
+ '&response_type=code'
+ '&client_id=' + CLIENT_ID
//+ '&login_hint=...%40gmail.com'
;
AuthRequest += Query;
Logger.log(AuthRequest);
return AuthRequest;
}
function cb(response) {
Logger.log(response);
}
When I click on the url generated by the generateAuthRequestURL()
it takes me to the consent screen where I click allow. But then every time I get 'The state token is invalid or has expired'.
The webapp is published and I have tested both the exec and dev versions with the same result. I have also tried with and without a login_hint.
I have also experimented with Apps-Script-Folder-Library as well as gdrive-appdata. I couldn't get the first one to work, and the second one I don't even know how to use.