【问题标题】:Use path/slug after Web App's base url in Google Apps Script在 Google Apps 脚本中的 Web 应用的基本 url 之后使用 path/slug
【发布时间】:2020-05-11 12:09:01
【问题描述】:

我希望通过在 Google Apps 脚本中添加如下路径来创建 URL:

https://script.google.com/macros/s/APP_ID/exec/fileName.txt

我怎样才能为 Web App 服务实现这一点?

【问题讨论】:

  • 请在您的问题中包含解释您的问题所需的所有内容...不要依赖外部链接
  • 我已经更新了我的帖子,请检查。希望这能消除误解,谢谢!

标签: google-app-engine google-apps-script google-api google-app-maker


【解决方案1】:

我相信你的目标如下。

  • 您希望使用https://script.google.com/macros/s/APP_ID/exec/fileName.txt 的 URL 访问 Web 应用程序。

对于这个,这个答案怎么样?我认为您可以使用 Web Apps 来实现您的目标。作为一个示例案例,我想在用户访问https://script.google.com/macros/s/APP_ID/exec/fileName.txt时使用下载文本文件的示例脚本来解释这一点。

用法:

请执行以下流程。

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 path = e.pathInfo;
  if (path == "filename.txt") {
    const sampleTextData = "sample";
    return ContentService.createTextOutput(sampleTextData).downloadAsFile(path);
  }
  return ContentService.createTextOutput("Wrong path.");
}
  • 要在https://script.google.com/macros/s/APP_ID/exec/fileName.txt 中检索fileName.txt 的值,请使用pathInfo
    • 例如,当您通过https://script.google.com/macros/s/APP_ID/exec/fileName.txt访问查看edoGet(e)时,您可以检索到{"contextPath":"","contentLength":-1,"parameter":{},"parameters":{},"queryString":"","pathInfo":"fileName.txt"}
  • 在这种情况下,使用 GET 方法。

3。部署 Web 应用程序。

  1. 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
  2. “执行应用程序为:”选择“我”
    • 由此,脚本以所有者身份运行。
  3. “谁有权访问应用程序:”选择“任何人,甚至匿名”
    • 在这种情况下,不需要请求访问令牌。我认为我建议您使用此设置来实现您的目标。
    • 当然,您也可以使用访问令牌。届时,请将其设为“任何人”。请在访问令牌中包含https://www.googleapis.com/auth/drive.readonlyhttps://www.googleapis.com/auth/drive 的范围。这些范围是访问 Web 应用所必需的。
  4. 单击“部署”按钮作为新的“项目版本”。
  5. 自动打开“需要授权”对话框。
    1. 点击“查看权限”。
    2. 选择自己的帐户。
    3. 点击“此应用未验证”中的“高级”。
    4. 点击“转到###项目名称###(不安全)”
    5. 点击“允许”按钮。
  6. 点击“确定”。
  7. 复制 Web 应用程序的 URL。就像https://script.google.com/macros/s/###/exec
    • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

4。使用 Web 应用程序运行函数。

请使用您的浏览器访问https://script.google.com/macros/s/###/exec/filename.txt。这样,一个文本文件就被下载了。

注意:

  • 当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。

参考资料:

【讨论】:

  • e.pathInfo 是我一直在寻找的东西。非常感谢您的详细解答。
猜你喜欢
  • 2012-10-03
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多