如何获取当前临时匿名活跃用户密钥及相关存储与比对方法?
Let’s tackle your questions about the anonymous user key returned by Session.getTemporaryActiveUserKey() when no user is logged in:
1. Storage Location of the Anonymous User Key
The anonymous temporary key is managed entirely by Google’s Apps Script session infrastructure. It’s stored server-side, tied to the anonymous session context, and there’s no exposed, accessible storage location that your script can directly read or modify.
On the client side, Google may use session cookies to track this anonymous context, but these are handled automatically by the browser and aren’t accessible to Apps Script code—since scripts run server-side, they don’t have direct access to client-side cookie data.
2. Retrieving the Current Anonymous Key for Comparison
Good news: You don’t need a separate method to get the current anonymous key—calling Session.getTemporaryActiveUserKey() itself in an anonymous session will return the active anonymous key.
Here’s how you can use this for comparison:
- When you need to check if the current session is anonymous and verify the key, simply invoke the method. If no user is logged in, it will return the current valid anonymous key (which rotates every 30 days automatically, as noted in the docs).
- To compare against a previously stored anonymous key, save the key value (e.g., in
PropertiesService) and then compare it to the value returned by a fresh call toSession.getTemporaryActiveUserKey().
Keep in mind that if the key has rotated (after 30 days), the new call will return the updated key, so your comparison will naturally detect the change.
内容的提问来源于stack exchange,提问作者Alberto Balistreri

