谷歌应用脚本网络服务上的 Cookie 或会话



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

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

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

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

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
    }
}

最新更新