0

During the past week I have faced a nasty issue with my google apps script, which has been working just fine for several years already, with at least 500 users.

When I'm trying to get the login information of the current user this way:

var email_act = Session.getActiveUser().getEmail();

the server throws an exception:

Exception: You do not have permission to call Session.getActiveUser. Required permissions: https://www.googleapis.com/auth/userinfo.email at getUserInfo(UserFunctions:46:27)

In my case the effective user is different than active user, but I got the same error also when trying to call Session.getEffectiveUser().

This happens randomly, approx 30% of calls and so far my solution was simply to retry until it succeeds. Note that when I did the retries in the backend-code, it did not solve the problem, maybe because there's something wrong with the "session" itself. If it failed once, it failed also all the consequent calls. Therefore, I implemented the retry-loop in front-end code (assuming that each call to back-end is then considered as a new session) and that helped - after a few (worst case 5) retries it works. The Back-end has now a simple try-catch error handler and it returns status (-1) to FE in case of failure.

Any ideas what might be the root cause? My solution is way too uggly to live with...

Thanks in advance!

  • 1
    Does this answer your question? [Google Apps Script: AuthMode gets confused w/ multiple logged in users](https://stackoverflow.com/questions/54751857/google-apps-script-authmode-gets-confused-w-multiple-logged-in-users) – Rubén Mar 01 '20 at 16:47
  • @Rubén, not really, as my user are logged from several machine/terminals. This might have something to do with the issue when user has logged in to several google accounts with the same browser, but that kind of issue was reported over a year ago, while my problems started roughly one month ago and everybody was impacted - including me, with just single chrome. – Jarno Tuominen Apr 03 '20 at 17:33

1 Answers1

0

I just came across the same error today. For me, the Session.getActiveUser().getEmail() was in a scriplet on a dynamic webpage I create with a script using the function "include" that is defined in the code.gs like this:

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

It seems changing this to

function include(filename) {
  return HtmlService.createTemplateFromFile(filename).evaluate().getContent();
}

has cleared things right up. I believe the error started occuring when I switched the code to the new V8 engine in GAS.

EDIT: oops, it worked the first time, but then failed after that. So I had to move the Session.getActiveUser().getEmail() inside code.gs

function getUserEmail() {
  return Session.getActiveUser().getEmail()
}

and in the javascript on the webpage I had to use a callback :

google.script.run.withSuccessHandler(function (email) {
  document.getElementById("yourIDhere").innerHTML = email;
}).getUserEmail();

Not very pretty at all, but it's working reliably now.

Raphael
  • 201
  • 2
  • 3