【问题标题】:Cookies or Session on Google Apps Script WebserviceGoogle Apps 脚本网络服务上的 Cookie 或会话
【发布时间】:2017-07-01 04:35:41
【问题描述】:

在服务器端代码可以访问浏览器上的 Cookie 或某种会话变量之前,我已经创建了网站。我还没有弄清楚如何用 GAS 做到这一点。我可以使用 GAS 托管一个网站,但是在加载新页面时我无法看到登录会话。我该怎么做?

我希望在 doGet() 事件中看到此信息,如下所示:

function doGet(e){
  e.session.userid; //or something like this
}

注意:任何人都可以访问我的网站,即使是匿名的。所以我不能依赖谷歌登录。

【问题讨论】:

  • 发布的 webapp 是作为开发者还是用户运行?
  • 看看 Session.getTemporaryActiveUserKey() developers.google.com/apps-script/reference/base/…
  • @SpencerEaston:它作为开发者运行。最终用户可能有也可能没有 Gmail 帐户,这不是必需的。
  • @AntonDementiev:我试过Session.getTemporaryActiveUserKey()。但是,这仅在用户登录到 Gmail 时才有效。如果用户没有登录到 Gmail,它会为“匿名”生成一个密钥,这对于所有匿名用户(即使在不同的 PC 上)都是相同的。我需要浏览器会话独有的东西。这就是我所需要的几乎

标签: session cookies google-apps-script


【解决方案1】:

Google 有一个 Session 方法。

https://developers.google.com/apps-script/reference/base/session

 // Log the email address of the person running the script (visiting the page).
 var email = Session.getActiveUser().getEmail();
 Logger.log(email);

编辑:

关于基于 cookie 的识别的评论。

我不是这方面的专家,但我的想法是做这样的事情

客户端 JS

$(function(){
    var clientCookie = document.cookie
    google.script.run
        .withSuccessHandler(function(cookieString){
        if(cookieString){
            document.cookie= cookieString
        }
    })
    .craeteCookie(clientCookie) // server function to create cryptographic cookie string  
})

服务器端

function createCookie(clientCookie){
    if(clientCookie){
        //check cookie is valid
    }
    else{
        // create client cookie
    }
}

【讨论】:

  • 不,这不起作用。这仅适用于 Gmail 用户。如果用户没有 Gmail 帐户,这将不起作用。正如我在原帖中所说,我需要支持这一点。
  • 抱歉没有意识到这一点。我偶然发现这篇文章正在为匿名用户寻找类似的解决方案。我也找不到任何有用的东西。我正在考虑实现我自己的基于 ID 的 cookie 识别。
  • 你将如何做到这一点?我所能找到的只是在客户端识别用户的方法......我需要在服务器端进行!你有办法解决这个问题吗?
猜你喜欢
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
相关资源
最近更新 更多