oauth
feishu.auth.oauth
¶
OAuthNamespace
¶
Bases: Namespace
用户身份授权(登录)能力,挂载于 client.oauth。
仅封装飞书用户 OAuth 的通用流程:构建授权页 URL、用授权码换取 user_access_token、
刷新该凭证,以及读取已登录用户的 user_info。本命名空间是无状态的,
每位用户或会话的凭证存储由调用方自行负责。
所有流程均要求客户端配置自建应用凭据 feishu.auth.credentials.InternalCredential, 否则相关方法会抛出 ValueError。
飞书文档
源代码位于: feishu/auth/oauth.py
| Python | |
|---|---|
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 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 | |
authorize_url
¶
authorize_url(redirect_uri: str, *, scope: str | list[str] | tuple[str, ...] | None = None, state: str | None = None, prompt: str | None = None) -> str
构建用户授权页 URL(纯字符串拼接,不发起任何网络请求)。
引导用户跳转到该地址完成授权后,飞书会携带授权码 code 回调至 redirect_uri,
再调用 feishu.auth.oauth.OAuthNamespace.exchange_code 换取 user_access_token。
登录站点(accounts 域名)在调用时才根据区域解析,因此未知区域只会在此处抛错,
而不会在客户端构造时抛错。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
授权完成后的回调地址,需与应用后台配置的重定向 URL 一致。 |
必需 |
|
str | list[str] | tuple[str, ...] | None
|
申请的权限范围,可为以空格分隔的字符串或字符串序列。默认不传。 |
None
|
|
str | None
|
透传的状态参数,回调时原样返回,常用于防 CSRF 与会话关联。默认不传。 |
None
|
|
str | None
|
授权页提示行为,例如 |
None
|
返回:
| 类型 | 描述 |
|---|---|
str
|
用户授权页的完整 URL。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
当客户端未配置 feishu.auth.credentials.InternalCredential, 或区域无法解析出 accounts 域名时抛出。 |
飞书文档
示例:
源代码位于: feishu/auth/oauth.py
exchange_code
async
¶
exchange_code(code: str, redirect_uri: str | None = None) -> NestedDict
用授权码换取 user_access_token。
授权回调拿到的 code 调用本方法换取用户访问凭证。应用凭据在请求体中传递,
因此该请求不携带 Bearer(token_type=None),且响应为非信封格式
(expect_envelope=False),直接返回包含 access_token、refresh_token、
expires_in 等字段的结果。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
授权回调中获得的登录预授权码。 |
必需 |
|
str | None
|
回调地址,需与换取授权码时使用的一致。默认不传。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
包含 |
NestedDict
|
|
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
当客户端未配置 feishu.auth.credentials.InternalCredential 时抛出。 |
FeishuAuthError
|
当授权码无效或已被使用等鉴权类错误时抛出, 参见 feishu.errors.FeishuAuthError。 |
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/oauth.py
refresh
async
¶
refresh(refresh_token: str) -> NestedDict
刷新 user_access_token。
飞书的刷新令牌为一次性且会轮换:响应中会返回一个全新的 refresh_token,
调用方必须持久化新令牌并丢弃旧令牌,否则后续刷新将失败。各项有效期
(expires_in、refresh_token_expires_in)均来自响应,不应硬编码。
与 feishu.auth.oauth.OAuthNamespace.exchange_code 一致,凭据在请求体中传递,
请求不携带 Bearer 且响应为非信封格式。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
当前有效的刷新令牌。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
包含新的 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
当客户端未配置 feishu.auth.credentials.InternalCredential 时抛出。 |
FeishuAuthError
|
当刷新令牌无效或已过期等鉴权类错误时抛出, 参见 feishu.errors.FeishuAuthError。 |
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/auth/oauth.py
user_info
async
¶
user_info(user_access_token: str) -> NestedDict
使用用户访问凭证读取已登录用户的资料。
该接口为信封格式({code, msg, data}),用户凭证以 Bearer 形式发送。
返回 data 数据体,并通过属性 raw_envelope 暴露原始信封(与 IM 命名空间一致)。
敏感字段(email、enterprise_email、mobile、user_id)仅在对应的用户权限
被申请且已授权时才会出现。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
用户访问凭证,由 feishu.auth.oauth.OAuthNamespace.exchange_code 换取。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
用户资料数据体,包含 |
NestedDict
|
原始信封可通过 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|