0

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: users@email.com

NOTES

I recently tried adding:

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

but this had no effect.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Wronski
  • 1,506
  • 3
  • 18
  • 37

1 Answers1

1

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • unfortunately, we currently are unable to use `Execute as: User accessing the web app` because, the app utilizes libraries, and users accessing the app do not have access to these libraries. – Wronski Feb 24 '21 at 00:44
  • @Wronski Thank you for replying. From your question, I couldn't notice you had used library for Web Apps. So I answered for your script in the question. This is due to my poor skill. I deeply apologize for this. In the current stage, when library is used in Web Apps, it is required to also share the library and the GAS project of Web Apps to the users. If you don't want to share them, how about merging the library to the GAS project of Web Apps? By this, the Web Apps with above settings can be used without sharing. If this was also not the direction you expect, I apologize again. – Tanaike Feb 24 '21 at 00:54
  • since the script is public, it cannot be shared with the users who use the script. Given the cost of maintaining parallel code bases for common scripts, we cannot merge the library script into the existing app. – Wronski Feb 24 '21 at 00:59
  • @Wronski Thank you for replying. I have to apologize for my poor English skill. I cannot understand about `Given the cost of maintaining parallel code bases for common scripts`. Can I ask you about the detail of it? – Tanaike Feb 24 '21 at 01:02
  • @Wronski By the way, when `Execute as: Me` is used for the Web Apps, the script of Web Apps is run by the owner. So I think that when you want to retrieve the information of each user, to set `Execute as: User accessing the web app` will approach to the goal. I think that this is the current specification using Web Apps. – Tanaike Feb 24 '21 at 01:07
  • @Wronski But from your actual situation (You are using a library for Web Apps. You cannot share the library and merge the library to Web Apps.), in the current stage, how about using Javascript&HTML instead of Google Apps Script. The simple sample script can be seen at [the official document](https://developers.google.com/drive/api/v3/quickstart/js). If this was also not the direction you expect, I apologize again. – Tanaike Feb 24 '21 at 01:07
  • "Given the cost of maintaining parallel code bases for common scripts" means that we want to use libraries so that we don't have to have the same code running in 2 different App Script projects. It seems like Google does not have an easy way to get a user email when using `Execute as: Me`, so I am going to try to build Google Sign In into the HTML page, with Google OAuth. – Wronski Feb 24 '21 at 08:45
  • @Wronski Thank you for replying. From your replying, I understood that you will use a HTML instead of Web Apps of Google Apps Script. About this, if you have any problems, please tell me. – Tanaike Feb 24 '21 at 09:06