Cookies or Session on Google Apps Script Webservice

倾然丶 夕夏残阳落幕 提交于 2020-05-02 04:32:04

问题


I've created websites before where the server-side code has access to either Cookies on the browser or some sort of Session variables. I have not figured out how to do this with GAS. I can host a website with GAS, but I have no way of seeing the login session when a new page is loaded. How do I do this?

I would expect to see this information in the doGet() event, like this:

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

NOTE: Anyone can access my site, even anonymous. So I can't rely on Google logins.


回答1:


Google has a Session method.

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);

Edit:

In regard to the comment about cookie based identification.

I am no expert at this but my thought was to do something like this

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

Server Side

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


来源:https://stackoverflow.com/questions/44573749/cookies-or-session-on-google-apps-script-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!