credentials
feishu.auth.credentials
¶
Credential
¶
Bases: ABC
应用凭据抽象基类。
凭据负责换取应用级访问凭证(tenant_access_token 或 app_access_token),
并为缓存提供一个能区分不同凭据、凭证类型与服务器地址的缓存键。
feishu.auth.tokens.TokenManager 依赖本接口完成凭证的获取与缓存。
自建应用请使用 feishu.auth.credentials.InternalCredential。
源代码位于: feishu/auth/credentials.py
cache_key
abstractmethod
¶
cache_key(token_type: str, base_url: str) -> str
InternalCredential
¶
Bases: Credential
自建应用凭据。
使用应用的 app_id 与 app_secret 换取 tenant_access_token 或
app_access_token。两个秘钥均在请求体中传递,因此换取凭证的请求本身不携带任何鉴权头。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
应用唯一标识,以 |
必需 |
|
str
|
应用秘钥。 |
必需 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
当 |
飞书文档
tenant_access_token (自建应用):
https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal
app_access_token (自建应用):
https://open.feishu.cn/document/server-docs/authentication-management/access-token/app_access_token_internal
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/credentials.py
| Python | |
|---|---|
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 | |
cache_key
¶
cache_key(token_type: str, base_url: str) -> str
生成自建应用凭证的缓存键。
缓存键由应用标识、凭证类型与服务器地址组成,确保不同应用、不同凭证类型、 不同区域之间的凭证互不冲突。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
凭证类型, |
必需 |
|
str
|
飞书开放平台服务器地址。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
str
|
唯一的缓存键。 |
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/credentials.py
fetch
async
¶
换取自建应用的访问凭证。
app_id 与 app_secret 在请求体中传递,因此该请求不携带任何鉴权头。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
Transport
|
用于发起请求的传输层。 |
必需 |
|
str
|
凭证类型, |
必需 |
返回:
| 类型 | 描述 |
|---|---|
tuple[str, int]
|
由访问凭证与有效期(秒)组成的二元组,有效期为正整数。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
当 |
FeishuError
|
当响应中的 |
飞书文档
tenant_access_token (自建应用):
| Text Only | |
|---|---|
1 | |
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/credentials.py
AppTicketStore
¶
Bases: Protocol
app_ticket 存储协议。
商店应用(ISV)无法直接用 app_id + app_secret 换取 app_access_token,
还需一个由飞书每隔约一小时通过 app_ticket 事件推送的 app_ticket。
实现该协议即可作为 feishu.auth.credentials.StoreCredential 的 app_ticket 存储后端:
收到并解密 app_ticket 事件后调用 set 写入,换取凭证时由凭据调用 get 读取。
默认实现为进程内存储 feishu.auth.credentials.InMemoryAppTicketStore;如需在多个进程或
实例间共享,可自行实现基于 Redis 等外部存储的后端。
源代码位于: feishu/auth/credentials.py
get
async
¶
InMemoryAppTicketStore
¶
进程内 app_ticket 存储。
feishu.auth.credentials.AppTicketStore 的默认实现,将 app_ticket 保存在进程内存中。
app_ticket 不会在进程间共享,进程退出后即失效;如需跨进程共享,请自行实现 AppTicketStore 协议。
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/credentials.py
StoreCredential
¶
Bases: Credential
商店应用(ISV)凭据。
商店应用无法直接用 app_id + app_secret 换取凭证,还需一个由飞书每隔约一小时通过
app_ticket 事件推送的 app_ticket。换取流程为:用 app_id + app_secret + app_ticket
换取 app_access_token,再用该 app_access_token + tenant_key 换取某租户的
tenant_access_token。所有秘钥与凭据均在请求体中传递,因此换取凭证的请求本身不携带任何鉴权头。
app_ticket 由 feishu.auth.credentials.AppTicketStore 提供:在收到并解密 app_ticket
事件后,调用 await store.set(app_id, app_ticket) 写入;若存储中没有可用的 app_ticket,
本凭据会先请求飞书重新推送(auth/v3/app_ticket/resend)再抛出 feishu.errors.FeishuError,
待 app_ticket 事件到达并写入后重试即可。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
应用唯一标识,以 |
必需 |
|
str
|
应用秘钥。 |
必需 |
|
str
|
租户唯一标识,用于换取对应租户的 |
必需 |
|
AppTicketStore | None
|
|
None
|
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
当 |
飞书文档
app_access_token (商店应用):
https://open.feishu.cn/document/server-docs/authentication-management/access-token/app_access_token
tenant_access_token (商店应用):
https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token
示例:
收到并解密 app_ticket 事件后写入存储,再据此构造客户端:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/credentials.py
| Python | |
|---|---|
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
cache_key
¶
cache_key(token_type: str, base_url: str) -> str
生成商店应用凭证的缓存键。
缓存键由应用标识、租户标识、凭证类型与服务器地址组成。其中租户标识确保不同租户的凭证
互不冲突——这是商店应用与自建应用的关键区别,缺少它会导致不同租户的 tenant_access_token
相互覆盖。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
凭证类型, |
必需 |
|
str
|
飞书开放平台服务器地址。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
str
|
唯一的缓存键。 |
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/credentials.py
fetch
async
¶
换取商店应用的访问凭证。
app 类型直接返回 app_access_token;tenant 类型先换取 app_access_token,
再用其与 tenant_key 换取对应租户的 tenant_access_token。所有秘钥与凭据均在请求体中
传递,因此这些请求均不携带任何鉴权头。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
Transport
|
用于发起请求的传输层。 |
必需 |
|
str
|
凭证类型, |
必需 |
返回:
| 类型 | 描述 |
|---|---|
tuple[str, int]
|
由访问凭证与有效期(秒)组成的二元组,有效期为正整数。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
当 |
FeishuError
|
当存储中没有可用的 |
飞书文档
app_access_token (商店应用):
https://open.feishu.cn/document/server-docs/authentication-management/access-token/app_access_token
tenant_access_token (商店应用):
https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token
示例:
| Python Console Session | |
|---|---|