开发工具/SDK 参考/Python SDK/配置访问密钥
配置访问密钥
更新于: 2026-06-25 19:29:14
通过 Python SDK 方式调用扣子编程 OpenAPI 时,需要在 SDK 请求中配置访问密钥,用于身份信息认证和权限校验。扣子编程 OpenAPI 提供个人访问密钥和 OAuth 两种鉴权方式,你可以选择当前业务场景适合的鉴权方式,并获取对应的访问密钥。
对于 OAuth 授权码等授权方式,Coze Python SDK 已经封装了这部分代码,并处理了不同的返回错误代码,简化你的操作。
扣子编程 OpenAPI 目前支持的鉴权方式如下。
| 访问密钥类型 | 鉴权方式 | 说明 | 示例文件 |
|---|---|---|---|
| 个人访问密钥(PAT) | 个人访问密钥(PAT) | Personal Access Token,简称 PAT。扣子编程中生成的个人访问令牌。PAT 生成与使用便捷,适用于测试环境调试等场景。每个令牌可以关联多个空间,并开通指定的接口权限。生成方式可参考添加个人访问令牌。 | examples/auth_pat.py |
|
服务访问令牌(SAT) |
服务访问令牌(SAT) |
Service Access Token(简称 SAT)是以服务身份创建的访问凭证,可长期有效访问扣子编程资源,通常用于服务/应用程序的身份验证和授权。生成方式可参考添加服务访问令牌。 SAT 的示例代码与 PAT 通用,可直接参考 PAT 的示例文件。 |
|
|
OAuth 认证 |
授权码授权 (Authorization Code Flow) |
适用于有显著前后端之分的应用程序授权场景。其中前端模块负责与用户交互,后端服务处理前端请求,与扣子编程授权服务器和 OpenAPI 交互。 实现流程可参考OAuth 授权码授权。 |
|
|
PKCE 授权 (Authorization Code Flow with PKCE) |
应用程序无后端服务,所有操作都发生在应用程序的前端。 实现流程可参考OAuth PKCE。 |
||
|
设备码授权 (Device Code Flow) |
应用程序无后端服务,所有操作都发生在应用程序的 Command Line,且 Command Line 无法提供“同意授权”的操作。 实现流程可参考OAuth 设备授权。 |
||
|
JWT 授权 (JWT Flow) |
应用程序服务端直接调用扣子编程 OpenAPI。 应用程序后端服务代理应用程序自己的用户获取身份凭据,应用程序用户基于凭据直接访问 OpenAPI。 实现流程可参考OAuth JWT 授权(开发者)。 |
如果选择使用个人访问密钥鉴权,你需要先申请一个个人访问密钥,并添加指定空间和权限。操作步骤可参考添加个人访问令牌。
扣子编程建议你通过环境变量的方式管理访问密钥,避免在代码中通过硬编码方式进行编程,以免密钥泄露、引发安全风险。配置环境变量之后,您可以在不修改代码的情况下,将动态的鉴权参数传递到对应的函数,实现便捷安全的身份认证。
export COZE_API_TOKEN="pat_****"
import os
from cozepy import Coze, TokenAuth, COZE_CN_BASE_URL
# 通过环境变量引入密钥,访问 coze.cn 服务
coze = Coze(auth=TokenAuth(os.getenv("COZE_API_TOKEN")), base_url=COZE_CN_BASE_URL)
如果选择使用 OAuth 授权码方式完成授权,可参考以下流程及示例代码。
import os
from cozepy import Coze, TokenAuth, WebOAuthApp, COZE_CN_BASE_URL
# Auth应用的客户端ID,创建 OAuth 应用时获取的客户端 ID。
web_oauth_client_id = os.getenv("COZE_WEB_OAUTH_CLIENT_ID")
# 客户端密钥
web_oauth_client_secret = os.getenv("COZE_WEB_OAUTH_CLIENT_SECRET")
# 访问 coze.cn 服务
web_oauth_app = WebOAuthApp(
client_id=web_oauth_client_id,
client_secret=web_oauth_client_secret,
base_url=COZE_CN_BASE_URL,
)
# redirect link
web_oauth_redirect_uri = os.getenv("COZE_WEB_OAUTH_REDIRECT_URI")
# Generate the authorization link and direct the user to open it.
url = web_oauth_app.get_oauth_url(redirect_uri=web_oauth_redirect_uri)
# Open the authorization link in your browser and authorize this OAuth App
# After authorization, you will be redirected to the redirect_uri with a code and state
# You can use the code to get the access token
code = 'mock code'
# After obtaining the code after redirection, the interface to exchange the code for a
# token can be invoked to generate the coze access_token of the authorized user.
oauth_token = web_oauth_app.get_access_token(redirect_uri=web_oauth_redirect_uri, code=code)
# use the access token to init Coze client
coze = Coze(auth=TokenAuth(oauth_token.access_token), base_url=COZE_CN_BASE_URL)
# When the token expires, you can also refresh and re-obtain the token
oauth_token = web_oauth_app.refresh_access_token(oauth_token.refresh_token)
如果选择使用 OAuth PKCE 方式完成授权,可参考以下流程及示例代码。
import os
from cozepy import PKCEOAuthApp, COZE_CN_BASE_URL
# OAuth应用的客户端ID,创建 OAuth 应用时获取的客户端 ID。
pkce_oauth_client_id = os.getenv("COZE_PKCE_OAUTH_CLIENT_ID")
# 重定向地址,创建 OAuth 应用时指定的重定向 URL。
pkce_oauth_redirect_uri = os.getenv("COZE_WEB_OAUTH_REDIRECT_URI")
pkce_oauth_app = PKCEOAuthApp(client_id=pkce_oauth_client_id, base_url=COZE_CN_BASE_URL)
# In the SDK, we have wrapped up the code_challenge process of PKCE. Developers only need
# to select the code_challenge_method.
code_verifier = "random code verifier"
url = pkce_oauth_app.get_oauth_url(
redirect_uri=web_oauth_redirect_uri,
code_verifier=code_verifier,
code_challenge_method="S256"
)
# Open the authorization link in your browser and authorize this OAuth App
# After authorization, you can exchange code_verifier for access token
code = 'mock code'
# After obtaining the code after redirection, the interface to exchange the code for a
# token can be invoked to generate the coze access_token of the authorized user.
oauth_token = pkce_oauth_app.get_access_token(
redirect_uri=web_oauth_redirect_uri, code=code, code_verifier=code_verifier
)
# use the access token to init Coze client
coze = Coze(auth=TokenAuth(oauth_token.access_token), base_url=COZE_CN_BASE_URL)
# When the token expires, you can also refresh and re-obtain the token
oauth_token = pkce_oauth_app.refresh_access_token(oauth_token.refresh_token)
如果选择使用 OAuth 设备码方式完成授权,可参考以下流程及示例代码。
import os
from cozepy import Coze, TokenAuth, DeviceOAuthApp, COZE_CN_BASE_URL
# OAuth应用的客户端ID,创建 OAuth 应用时获取的客户端 ID。
device_oauth_client_id = os.getenv("COZE_DEVICE_OAUTH_CLIENT_ID")
device_oauth_app = DeviceOAuthApp(client_id=device_oauth_client_id, base_url=COZE_CN_BASE_URL)
# First, you need to request the server to obtain the device code required in the device auth flow
device_code = device_oauth_app.get_device_code()
# The returned device_code contains an authorization link. Developers need to guide users
# to open up this link.
# open device_code.verification_url
try:
oauth_token = device_oauth_app.get_access_token(
device_code=device_code.device_code,
poll=True,
)
except CozePKCEAuthError as e:
if e.error == CozePKCEAuthErrorType.ACCESS_DENIED:
# The user rejected the authorization.
# Developers need to guide the user to open the authorization link again.
pass
elif e.error == CozePKCEAuthErrorType.EXPIRED_TOKEN:
# The token has expired. Developers need to guide the user to open
# the authorization link again.
pass
else:
# Other errors
pass
raise # for example, re-raise the error
# use the access token to init Coze client
coze = Coze(auth=TokenAuth(oauth_token.access_token), base_url=COZE_CN_BASE_URL)
# When the token expires, you can also refresh and re-obtain the token
oauth_token = device_oauth_app.refresh_access_token(oauth_token.refresh_token)
如果选择使用 OAuth JWT 方式完成授权,可参考以下流程及示例代码。
import os
from cozepy import Coze, TokenAuth, JWTOAuthApp, COZE_CN_BASE_URL
# OAuth应用的客户端ID,创建 OAuth 应用时获取的客户端 ID。
jwt_oauth_client_id = os.getenv("COZE_JWT_OAUTH_CLIENT_ID")
# OAuth 应用的私钥,创建 OAuth 应用时获取的私钥。
jwt_oauth_private_key = os.getenv("COZE_JWT_OAUTH_PRIVATE_KEY")
# OAuth 应用的公钥指纹,可以在 OAuth 应用页面找到这个应用,在操作列单击编辑图标,进入配置页面查看公钥指纹。
jwt_oauth_public_key_id = os.getenv("COZE_JWT_OAUTH_PUBLIC_KEY_ID")
jwt_oauth_app = JWTOAuthApp(
client_id=jwt_oauth_client_id,
private_key=jwt_oauth_private_key,
public_key_id=jwt_oauth_public_key_id,
base_url=COZE_CN_BASE_URL
)
# The jwt process does not require any other operations, you can directly apply for a token
oauth_token = jwt_oauth_app.get_access_token(ttl=3600)
# use the access token to init Coze client
coze = Coze(auth=TokenAuth(oauth_token.access_token), base_url=COZE_CN_BASE_URL)
# The jwt oauth process does not support refreshing tokens. When the token expires,
# just directly call get_access_token to generate a new token.