我相信你的目标如下。
- 您想通过使用 Gmail API 访问像
https://example_api/?action=send&to=someone@gmail.com&message=hello&auth_user=myuser@gmail.com&pass=xyz-app-pass 这样的 URL 来发送电子邮件。
我认为您的目标可以使用由 Google Apps 脚本创建的 Web 应用程序来实现。在这种情况下,脚本可能很简单。所以在这个答案中,我想建议使用 Google Apps Script 创建的 Web Apps 来实现您的目标。
用法:
请执行以下流程。
1。创建 Google Apps 脚本的新项目。
Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。
如果要直接创建,请访问https://script.new/。在这种情况下,如果您没有登录 Google,则会打开登录屏幕。所以请登录谷歌。这样,Google Apps Script 的脚本编辑器就打开了。
2。准备脚本。
请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。此脚本适用于 Web 应用程序。
function doGet(e) {
const allowedUsers = [
{email: "myuser@gmail.com", password: "xyz-app-pass"},
,
,
,
]; // If you want to allow other users to use this script, please add other emails and passwords to the `allowedUsers` array;
const subject = "sample subject";
const {action, to, message, auth_user, pass} = e.parameter;
if (action == "send" && allowedUsers.some(({email, password}) => email == auth_user && password == pass)) {
MailApp.sendEmail({to: to, subject: subject, body: message});
return HtmlService.createHtmlOutput("Email was sent.");
}
return HtmlService.createHtmlOutput("Email was not sent.");
}
3。部署 Web 应用程序。
- 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
- 为“执行应用程序为:”选择“用户访问网络应用程序”。
- 这样,脚本以访问 Web 应用程序的用户身份运行。
- 为“谁有权访问应用程序:”选择“任何人”。。
- 在这种情况下,请使用浏览器访问 Web 应用程序。这样,当您登录 Google 时,就可以使用 Web Apps。
- 并且,在此设置中,当“用户 A”通过登录 Google 访问 Web 应用程序时,“用户 A”也可以使用自己的 Gmail 发送电子邮件。在这种情况下,请将“用户A”的
{email: "###", password: "###"} 添加到脚本中的allowedUsers。这样,只有注册用户才能发送电子邮件。
- 如果不想分享给其他用户,请设置
Execute the app as: Me和Who has access to the app: Only myself。
- 单击“部署”按钮作为新的“项目版本”。
- 自动打开“需要授权”对话框。
- 点击“查看权限”。
- 选择自己的帐户。
- 点击“此应用未验证”中的“高级”。
- 点击“转到###项目名称###(不安全)”
- 点击“允许”按钮。
- 点击“确定”。
- 复制 Web 应用程序的 URL。就像
https://script.google.com/macros/s/###/exec。
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
4。测试网络应用。
请通过包含如下查询参数,使用您的浏览器访问您的 Web 应用程序的 URL。当您已经登录 Google 后,就会运行 Web Apps 的脚本。
https://script.google.com/macros/s/###/exec?action=send&to=someone@gmail.com&message=hello&auth_user=myuser@gmail.com&pass=xyz-app-pass
结果:
当运行上述脚本并且allowedUsers 中包含auth_user 和pass 时,返回Email was sent.。
注意:
- 当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。
- 如果您想使用脚本和 curl 访问 Web 应用程序,则需要使用访问令牌访问它。在这种情况下,include the scope 的
https://www.googleapis.com/auth/drive.file (在某些情况下,可能工作 /drive 或 /drive.readonly)
- Google Apps 脚本是一个简单的示例脚本,用于解释实现目标的方法。所以请根据您的实际情况进行修改。
参考资料: