Java 后端面试题库 / 全部题解免费阅读理解原理,有条理地回答

DeepSeek Chat Completions 的认证头怎样写?

查看完整题解

参考回答

直接回答

用 HTTP 头 Authorization: Bearer <API_KEY>。OpenAI 兼容 SDK 会按所配置的 api_key 帮你构造。其他兼容协议的认证形式应按各自文档核对。

讲清原理

认证信息应由服务端持有。遇到 401 先查密钥来自哪个环境、是否过期或被撤销、请求是否走错服务入口。

例子与易错点

下面只构造一条带模拟值的请求,确认凭据位于 HTTP 头,且没有放进 JSON。

import httpx

request = httpx.Request(
    "POST", "https://api.deepseek.com/chat/completions",
    headers={"Authorization": "Bearer local-test-key"},
    json={"model": "deepseek-flash", "messages": [{"role": "user", "content": "你好"}]},
)
assert request.headers["Authorization"] == "Bearer local-test-key"
assert b"local-test-key" not in request.content
print("the test credential exists only in the header")

相关题:第 6 题、第 7 题、第 25 题、第 26 题、第 30 题。

这道题,你掌握得怎么样?

掌握程度由你主动标记,阅读和收藏不会自动改变状态。