3

I've developed a web app with google script and code this in my gs.

var email = Session.getActiveUser().getEmail();
Logger.log(email);
// then do something to render the Email address on the page

After publishing the script, I log in with another account and execute the script.

Then the page display this:

This application was created by another user, not by Google.

This application has access to the following personal information: email address.

But still nothing in Log and the page display nothing.

I just don't get it...

Rubén
  • 34,714
  • 9
  • 70
  • 166
qdwang
  • 425
  • 1
  • 4
  • 13
  • Your apps script application is hosted in Normal Gmail Account or a Google Apps Enterprise account? Also, what settings did you use to publish the app? – Waqar Ahmad Mar 27 '13 at 06:15
  • I'm not sure that log works for scripts running as web-apps like this. Have you tried to save the email on ScriptProperties, or send it to you via email? – Henrique G. Abreu Mar 27 '13 at 11:37
  • I hosted in my account and I set "Execute the app as myself" and "Who has access to the app: anyone" – qdwang Mar 27 '13 at 14:00
  • When developing the script, I can get the Email address of my self. When it is published, my email address can display, but no others can. – qdwang Mar 27 '13 at 14:03
  • Have you tried to set execution permissions to the active user, instead of "execute the app as myself"? – nathangiesbrecht Mar 27 '13 at 14:22
  • Nope, I just can't. Because in the script, I'm going to access a spreadsheet which is private. So I can only execute the app as myself. – qdwang Mar 28 '13 at 02:00
  • Did you find a solution for this? I have a similar problem: http://stackoverflow.com/questions/27572388/how-to-get-user-email-of-a-google-spreadsheet-add-on –  Dec 20 '14 at 00:20
  • @WaqarAhmad What difference does it make? – eceo Nov 05 '21 at 07:48

5 Answers5

3

Although it's not stated explicitly in the reference documentation for Session.GetActiveUser(), my testing confirmed that your web app needs to execute as the user accessing the web app to have access to getActiveUser(). I used the code below and the logging library described here.

Depending on the use case of your application, perhaps you could create a library containing a centralized ScriptDB instance to capture the active user email. Then, create another project that works with your private spreadsheet and the same scriptDB library.

var LOG_FILENAME = 'your-log-doc'
var LOG_PATH = 'folder/subfolder/third-folder'


function doGet() {
  //Change variable above to a good path and filename for you
  var x = LogLibrary.InitializeLogging(LOG_PATH, LOG_FILENAME)
  Logger.log(x)

  var email = Session.getActiveUser().getEmail();
  Logger.log("Start email logging")
  Logger.log(email);
  LogLibrary.fnSaveLog()  //Write the save and flush the logger content

  // then do something to render the Email address on the page
  var HTMLToOutput = '<html><h1>A header</h1><p>' + email + '</p></html>';
  return HtmlService.createHtmlOutput(HTMLToOutput);
}
  • 1
    The second link here redirect to a sunset schedule, so I don't think that would work anymore. – Stykes May 26 '20 at 19:37
1

I've made some tests because I am looking for a solution about the same problem,and I conclude that :

  • so that function can works, the web app should be executed as "user accessing the web app", and for that, the web app should be configured so that can be executed as user accessing the web app
  • Note that : the user will be asked to authorise the script to access, edit and manage it's drive also receiving and sending mails (which is not evident !! for all most of users of corse").

In the end that it depend of what you will use it for.

And in google documentation nothing is mentioned about this issue!!

For a simple function that returns the email address as a text to use as script or in google html forms.. etc :

function onOpen(){
var emailName = Session.getActiveUser().getEmail();
var optionsHtml =  emailName.toString().split(",");

     return optionsHtml;
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
0

Look in the gas issues forum (google). It hax been addressed many times. Basically you need to set it as run as user accessing the app if u expect users from outside your domain. Changing that setting will qlikely break your app depending on the permissions it needs

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
-1

If your app is from before the 25th of June 2013, and you are using getEffectiveUser() or getUser() methods you will need to re-authorize the script because of the new authorization experience.

You can see the documentation here.

https://developers.google.com/apps-script/scripts_google_accounts#understandingPermissions

And do not forget to execute the script before deploying.

Dade
  • 503
  • 3
  • 12
-1

If you want to access the user's email, make sure you have added the "https://www.googleapis.com/auth/userinfo.email" email access policy in the appsscript .json

You can see the documentation here.

https://developers.google.com/apps-script/reference/base/session#authorization