0
votes

CURRENTLY

I want to collect the user email of users accessing my Google Apps Script webapp.

To achieve this I set Execute as "Me", with Who has access: "Anyone with Google account"

function doGet(e) {
 let userEmail = Session.getActiveUser().getEmail()
 return HtmlService.createHtmlOutput(
    "<!DOCTYPE html><html><body>Email: "+userEmail+"</body></html>"
 );
}

The deployment then has an EXEC link created:

https://script.google.com/macros/s/1234................56789/exec

ISSUE

When the execution link is accessed by a user in Incognito window, IE, or Firefox, the user is asked to log in on Google.

After the user logs in, the page returns:

Email:

WHAT I WANT

  1. User access exec link, and is prompted to log into google
  2. After logging into google, user receives HTML:

Email: [email protected]

NOTES

I recently tried adding:

  "oauthScopes": [
    "https://www.googleapis.com/auth/userinfo.email"
  ],

but this had no effect.

1

1 Answers

1
votes

I believe your goal as follows.

  • When an user is logged in Google and access to the Web Apps, you want to retrieve the user's email address.

Modification points:

  • From your question, I confirmed that you deployed the Web Apps as Execute as: Me and Who has access to the app: Anyone with Google account. In this case, in order to achieve your goal, please deploy the Web Apps as follows.

    • Execute as: User accessing the web app and Who has access to the app: Anyone with Google account
  • And also, in this case, please redeploy the Web Apps as new version.

After you reflected above settings to the Web Apps, please test it again. In my environment, when an user who is not the owner of Web Apps accesses to the Web Apps, the user's email can be shown by your script. But, the user accesses to the Web Apps for the 1st time, it is required to authorize the scope of https://www.googleapis.com/auth/userinfo.email.

Note:

  • If above settings didn't resolve your issue, as a test, please remove "oauthScopes": ["https://www.googleapis.com/auth/userinfo.email"], from appsscript.json and test it again.

References: