Session.getActiveUser().getEmail() returns empty if user is on a different Google domain

1,576 views
Skip to first unread message

James Lenthall

unread,
Jul 8, 2019, 8:00:07 AM7/8/19
to Google Apps Script Community
I work for Portsmouth University and using Google app script to create a sheets based application. I have who has access set to anyone so any person with a Google account can access the page.

I then check through a list of email addresses against the active user to show the content relevant to that account.

My problem is this, I'm on a the domain port.ac.uk, anyone using an email on this domain can be found with Session.getActiveUser().getEmail().

Students on the other hand are on myport.ac.uk or anyone on gmail.com, Session.getActiveUser().getEmail() returns empty.

Is there anyway I can get it to pick up the logged in user email without manual input?

Dimu Designs

unread,
Jul 8, 2019, 11:06:49 AM7/8/19
to google-apps-sc...@googlegroups.com
I assume you are an admin for your google domain, that's why you can get the emails of users under that domain. For accounts not under your institution's GSuite domain you'll need to request more permissions so that users can grant your web app access to their personal info. 

First you'll need to know the existing scopes already associated with you web-app. You can find that under the "Scopes" tab when you go to  "File" menu and select  "Project Properties". Record those scopes. Then you'll need to edit your manifest via the "View" menu and select "show manifest". You'll need to add those oauth scopes to that manifest file and also add the following scope (its formatted as a URL):

"https://www.googleapis.com/auth/userinfo.email"

For more info on manifests and scopes check out the links below:

https://developers.google.com/apps-script/concepts/manifests

James Lenthall

unread,
Jul 8, 2019, 11:14:22 AM7/8/19
to google-apps-sc...@googlegroups.com
I was just about to update this as I've found a work around. But that is all extremely useful to know thank you very much.

I've simply used Google sign in button for websites (https://developers.google.com/identity/sign-in/web/) that way domain isn't an issue and users signed in with multiple accounts can choose which to run the page as, where as Google App Script's Session.getActiveUser().getEmail() only ever returns the first logged in.

Once again thank you for the response, and how swiftly you've given a solution.

Ryan Yee

unread,
Dec 15, 2022, 1:01:58 PM12/15/22
to Google Apps Script Community
Sorry to revive this thread, I am having the same issue where emails not on our specific domain returns empty. 

1) To DimuDesigns' recommendation, I confirmed that the scopes outlined are all already included 
2) James, I have an AppScript that is built into sheets, how would I implement the Google Sign in button in this case? 

Are there any other suggestions recommendations now that sign-in with web is being deprecated? 

Thanks in advance
Reply all
Reply to author
Forward
0 new messages