SerializableTokenCache 类
此序列化可以是实现自己的持久性的起点。
此类实际上不会在磁盘/db/etc 上保留缓存。 根据需求,基于文件的未加密持久性的以下简单方案可能足够:
import os, atexit, msal
cache_filename = os.path.join( # Persist cache into this file
os.getenv(
# Automatically wipe out the cache from Linux when user's ssh session ends.
# See also https://github.com/AzureAD/microsoft-authentication-library-for-python/issues/690
"XDG_RUNTIME_DIR", ""),
"my_cache.bin")
cache = msal.SerializableTokenCache()
if os.path.exists(cache_filename):
cache.deserialize(open(cache_filename, "r").read())
atexit.register(lambda:
open(cache_filename, "w").write(cache.serialize())
# Hint: The following optional line persists only when state changed
if cache.has_state_changed else None
)
app = msal.ClientApplication(..., token_cache=cache)
...
或者,可以使用更复杂的缓存持久性库、 MSAL 扩展,该库提供令牌缓存持久性和加密等。
构造函数
SerializableTokenCache()
变量
| 名称 | 说明 |
|---|---|
|
has_state_changed
|
指示内存中的缓存状态自上次 serialize 或 deserialize 调用以来是否已更改。 |
方法
| add | |
| deserialize |
从以前通过 serialize 获取的状态反序列化缓存() |
| modify | |
| serialize |
将当前缓存状态序列化为字符串。 |
add
add(event, **kwargs)
参数
| 名称 | 说明 |
|---|---|
|
event
必需
|
|
deserialize
从以前通过 serialize 获取的状态反序列化缓存()
deserialize(state: str | None) -> None
参数
| 名称 | 说明 |
|---|---|
|
state
必需
|
|
modify
modify(credential_type, old_entry, new_key_value_pairs=None)
参数
| 名称 | 说明 |
|---|---|
|
credential_type
必需
|
|
|
old_entry
必需
|
|
|
new_key_value_pairs
|
默认值: None
|
serialize
将当前缓存状态序列化为字符串。
serialize() -> str
属性
has_state_changed
has_state_changed = False