【问题标题】:Google Apps Script doGet(e) get user emailGoogle Apps 脚本 doGet(e) 获取用户电子邮件
【发布时间】:2021-05-26 05:23:13
【问题描述】:

目前

我想收集访问我的 Google Apps 脚本网络应用的用户的用户电子邮件。

为了实现这一点,我将执行设置为“我”,谁有权访问:“任何拥有 Google 帐户的人”

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

然后部署会创建一个 EXEC 链接:

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

问题

当用户在隐身窗口、IE 或 Firefox 中访问执行链接时,会要求用户登录 Google。

用户登录后,页面返回:

Email:

我想要什么

  1. 用户访问exec链接,并提示登录google
  2. 登录谷歌后,用户收到 HTML:

Email: users@email.com

注意事项

我最近尝试添加:

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

但这没有任何效果。

【问题讨论】:

    标签: google-apps-script web-applications


    【解决方案1】:

    我相信你的目标如下。

    • 当用户登录 Google 并访问 Web 应用程序时,您希望检索用户的电子邮件地址。

    修改点:

    • 根据您的问题,我确认您将 Web 应用程序部署为 Execute as: MeWho has access to the app: Anyone with Google account。在这种情况下,为了实现您的目标,请按如下方式部署 Web 应用程序。

      • Execute as: User accessing the web appWho has access to the app: Anyone with Google account
    • 另外,在这种情况下,请将 Web 应用程序重新部署为新版本。

    在您将上述设置反映到 Web 应用程序后,请再次测试。在我的环境中,当不是 Web 应用程序所有者的用户访问 Web 应用程序时,您的脚本可以显示用户的电子邮件。但是,用户第一次访问Web Apps,需要授权https://www.googleapis.com/auth/userinfo.email的范围。

    注意:

    • 如果上述设置未能解决您的问题,作为测试,请从appsscript.json 中删除"oauthScopes": ["https://www.googleapis.com/auth/userinfo.email"], 并重新测试。

    参考资料:

    【讨论】:

    • 很遗憾,我们目前无法使用Execute as: User accessing the web app,因为该应用使用了库,访问该应用的用户无权访问这些库。
    • @Wronski 感谢您的回复。根据您的问题,我没有注意到您使用了 Web 应用程序库。所以我在问题中回答了你的脚本。这是因为我的技术不好。我对此深表歉意。现阶段,在Web Apps中使用库时,还需要将Web Apps的库和GAS项目共享给用户。如果不想分享,不如把库合并到 Web Apps 的 GAS 项目中?这样,具有上述设置的 Web 应用程序可以在不共享的情况下使用。如果这也不是您期望的方向,我再次道歉。
    • 由于该脚本是公开的,因此无法与使用该脚本的用户共享。考虑到维护通用脚本并行代码库的成本,我们无法将库脚本合并到现有应用程序中。
    • @Wronski 感谢您的回复。我不得不为我糟糕的英语水平道歉。我无法理解Given the cost of maintaining parallel code bases for common scripts。可以问一下具体情况吗?
    • @Wronski 顺便说一句,当Execute as: Me 用于Web Apps 时,Web Apps 的脚本由所有者运行。所以我认为当你想检索每个用户的信息时,设置Execute as: User accessing the web app会接近目标。我认为这是当前使用 Web 应用程序的规范。
    猜你喜欢
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    相关资源
    最近更新 更多