Google Apps Script doGet(e) 获取用户电子邮件 [英] Google Apps Script doGet(e) get user email
问题描述
当前
我想收集访问我的 Google Apps Script 网络应用程序的用户的用户电子邮件.
I want to collect the user email of users accessing my Google Apps Script webapp.
为了实现这一点,我将执行设置为我",谁有权访问:任何拥有 Google 帐户的人"
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>"
);
}
然后部署会创建一个 EXEC 链接:
The deployment then has an EXEC link created:
https://script.google.com/macros/s/1234................56789/exec
问题
当用户在隐身窗口、IE 或 Firefox 中访问执行链接时,会要求用户登录 Google.
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:
邮箱:
我想要什么
- 用户访问
exec
链接,并提示登录google - 登录谷歌后,用户收到 HTML:
- User access
exec
link, and is prompted to log into google - After logging into google, user receives HTML:
邮箱:users@email.com
注意事项
我最近尝试添加:
"oauthScopes": [
"https://www.googleapis.com/auth/userinfo.email"
],
但这没有效果.
推荐答案
我相信你的目标如下.
- 当用户登录 Google 并访问网络应用程序时,您希望检索用户的电子邮件地址.
根据您的问题,我确认您将 Web 应用程序部署为
执行身份:我
和谁有权访问该应用程序:任何拥有 Google 帐户的人
.在这种情况下,为了实现您的目标,请按如下方式部署 Web 应用程序.
From your question, I confirmed that you deployed the Web Apps as
Execute as: Me
andWho 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.
执行身份:访问网络应用的用户
和有权访问应用的人:任何拥有 Google 帐户的人
此外,在这种情况下,请将 Web 应用程序重新部署为新版本.
And also, in this case, please redeploy the Web Apps as new version.
在您将上述设置反映到 Web Apps 后,请再次测试.在我的环境中,当不是 Web Apps 所有者的用户访问 Web Apps 时,您的脚本可以显示用户的电子邮件.但是,用户第一次访问Web Apps,需要对https://www.googleapis.com/auth/userinfo.email
的范围进行授权.
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
.
- 如果上述设置不能解决您的问题,作为测试,请删除
oauthScopes":[https://www.googleapis.com/auth/userinfo.email"],
来自appsscript.json
并再次测试.
- If above settings didn't resolve your issue, as a test, please remove
"oauthScopes": ["https://www.googleapis.com/auth/userinfo.email"],
fromappsscript.json
and test it again.
这篇关于Google Apps Script doGet(e) 获取用户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!