qdwang
qdwang

Reputation: 445

google script Session.getActiveUser().getEmail() not working

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...

Upvotes: 3

Views: 20537

Answers (5)

Kamal AZIZ
Kamal AZIZ

Reputation: 19

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;
}

Upvotes: 1

Joko Priyono
Joko Priyono

Reputation: 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

Upvotes: -1

Dade
Dade

Reputation: 513

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.

Upvotes: -1

Zig Mandel
Zig Mandel

Reputation: 19835

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

Upvotes: 0

Steve Whisenhant
Steve Whisenhant

Reputation: 152

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);
}

Upvotes: 3

Related Questions