【问题标题】:Add URL parameters in Google Script Web apps在 Google Script Web 应用程序中添加 URL 参数
【发布时间】:2017-10-13 17:00:29
【问题描述】:

现在尝试了几个小时 - 有没有办法在已部署的 Web 应用程序中向服务器端 Google 脚本的 URL 添加参数,对任何人(不仅是 Google 帐户)开放?

我想在用户登录时存储一个密钥,这样用户就不必在每次刷新浏览器时再次登录。假设我当前的网址是: https://script.google.com/a/macros.../exec

我可以在登录时以某种方式添加 GAS 功能,以便将 URL 修改为类似 https://script.google.com/a/macros.../exec?key=abcdef&name=John%20Doe

这样,如果 doGet(e) 函数再次运行(网站重新加载)并且临时生成的密钥与我数据库中的用户名匹配,则用户将被定向到页面 B,如果密钥不匹配(或已在时间触发器上被删除)用户将被定向到页面 A(登录页面)?

我还试图找到将某些内容放入本地缓存的方法,但也无法弄清楚。Google CacheService 似乎只将密钥存储在我作为用户而不是客户端。

我想要实现的示例:

  function doGet(e){
    var name = e.parameter.name, output;
    if(name === undefined){
      /*/
      Add parameter name=john to URL.. 
      obviously what I intend to is to create a HtmlOutput on a login-page
      and then in a later (function login()) add this parameter there and not
      here, but if anyone can work this out for me I got the solution
     /*/
     } else {
     output = HtmlService.createHtmlOutput('Welcome back ' + name);
     }
    return output;
    }

【问题讨论】:

  • 我想你可能想看看PropertyService
  • @Cooper 谢谢我会尽快尝试这个 - 一直在尝试用谷歌搜索,但无法找到 PropertiesService 和 CacheService 的比较 - 两者都有 userCache /userProperties 但是当我使用它时CacheService userCache 充当脚本(我)的执行者,因为这就是我的网络应用程序的设置方式 - 即使应用程序已部署,因此“任何人,甚至匿名”都可以访问它。也许我应该将网络应用程序设置为充当“用户访问脚本”..?
  • 尝试了 PropertiesService,它甚至在 setUserProperties 上也充当 CacheService,可能是因为脚本以我自己(脚本所有者)的身份执行..
  • 你试过this solution吗?

标签: javascript google-apps-script


【解决方案1】:

这是一个可能的解决方案,它根据提供的 URL 参数将用户名保存到 PropertiesService,如果已经存在,则获取密钥。

function doGet(e) {
  //use the session service to get the active users email
  var currentUser = e.parameter.name;

  var key = e.parameter.key;

  var props = PropertiesService.getUserProperties().getProperty(currentUser);

  if(props) {
    //do something with the key
  } else {
    //if there is no username key in the properties service, then add it
    PropertiesService.getUserProperties().setProperty(currentUser, key);
  }
}

这里是文档:

https://developers.google.com/apps-script/reference/properties/properties-service#getUserProperties()

【讨论】:

  • webapp 对任何人开放,因此它不仅登录了访问 webapp 的 Google 客户,还登录了任何人。所以不幸的是,这也不会这样做 =/
  • 我明白了。我更改了上面的代码以反映使用查询字符串中提供的 name 参数到 doGet 函数。这有帮助吗?
  • URL 从一开始就没有任何参数,我想要实现的是在以后的函数中以编程方式向 URL 添加一个参数。我对 Stackoverflow 有点新,但会编辑我的原始发布以包含一些要显示的代码.. 给我 5 分钟
猜你喜欢
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多