tokens
feishu.auth.tokens
¶
CachedToken
dataclass
¶
缓存中的访问凭证。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
访问凭证。 |
必需 |
|
float
|
凭证的过期时刻,与 feishu.auth.tokens.TokenManager 使用的时钟同源。 |
必需 |
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/tokens.py
| Python | |
|---|---|
TokenCache
¶
Bases: Protocol
访问凭证缓存协议。
实现该协议即可作为 feishu.auth.tokens.TokenManager 的凭证缓存后端。 默认实现为进程内缓存 feishu.auth.tokens.InMemoryTokenCache;如需在多个进程或 实例间共享凭证,可自行实现基于 Redis 等外部存储的缓存。
源代码位于: feishu/auth/tokens.py
get
async
¶
get(key: str) -> CachedToken | None
set
async
¶
set(key: str, token: CachedToken) -> None
InMemoryTokenCache
¶
进程内访问凭证缓存。
feishu.auth.tokens.TokenCache 的默认实现,将凭证保存在进程内存中。
凭证不会在进程间共享,进程退出后即失效;如需跨进程共享,请自行实现 TokenCache 协议。
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/tokens.py
get
async
¶
get(key: str) -> CachedToken | None
set
async
¶
set(key: str, token: CachedToken) -> None
TokenManager
¶
应用级访问凭证管理器。
在凭据与传输层之上提供凭证的缓存、过期前刷新与并发去重:同一凭证只会被换取一次并复用,
临近过期时自动重新换取,且一批并发调用只会触发一次换取(避免惊群)。
不同凭证类型(如 tenant 与 app)使用各自独立的锁,互不阻塞。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
Credential
|
用于换取凭证的应用凭据,参见 feishu.auth.credentials.Credential。 |
必需 |
|
Transport
|
用于发起请求的传输层。 |
必需 |
|
TokenCache | None
|
凭证缓存后端。默认为 feishu.auth.tokens.InMemoryTokenCache。 |
None
|
|
int
|
过期前的提前刷新秒数,凭证在距过期不足该秒数时即重新换取。 |
TOKEN_REFRESH_OFFSET
|
|
int
|
扣除 |
MIN_TOKEN_TTL
|
|
Callable[[], float]
|
返回当前时刻的单调时钟函数,主要用于测试注入。 |
monotonic
|
飞书文档
tenant_access_token:
https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token
app_access_token:
https://open.feishu.cn/document/server-docs/authentication-management/access-token/app_access_token
源代码位于: feishu/auth/tokens.py
| Python | |
|---|---|
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
tenant_access_token
async
¶
tenant_access_token() -> str
获取租户访问凭证(tenant_access_token)。
等价于 token("tenant"),命中缓存且未临近过期时直接复用,否则重新换取。
返回:
| 类型 | 描述 |
|---|---|
str
|
租户访问凭证。 |
飞书文档
tenant_access_token:
https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token
示例:
源代码位于: feishu/auth/tokens.py
token
async
¶
token(token_type: str = 'tenant') -> str
获取指定类型的应用级访问凭证。
命中缓存且距过期不少于 refresh_offset 秒时直接复用缓存值,否则换取新凭证并写入缓存。
同一凭证类型上的并发调用会在锁内做双重检查,最终只触发一次换取。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
凭证类型, |
'tenant'
|
返回:
| 类型 | 描述 |
|---|---|
str
|
对应类型的访问凭证。 |
飞书文档
tenant_access_token:
https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token
app_access_token:
https://open.feishu.cn/document/server-docs/authentication-management/access-token/app_access_token
示例: