401, auth error code: invalid_api_key
错误信息
unexpected status 401 Unauthorized: Incorrect API key provided: sk-ae9e7*******************************************************b6e6. You can find your API key at https://platform.openai.com/account/api-keys., url: https://api.openai.com/v1/responses, cf-ray: a31b927e0bd3fa49-SJC, request id: req_c0876a23e85f4e6aa22220775c774a97, auth error: 401, auth error code: invalid_api_key
AI 解决方案
🔍 错误分析报告
1. 🌐 错误翻译
原文:unexpected status 401 Unauthorized: Incorrect API key provided: sk-ae9e7*******************************************************b6e6. You can find your API key at https://platform.openai.com/account/api-keys., url: https://api.openai.com/v1/responses, cf-ray: a31b927e0bd3fa49-SJC, request id: req_c0876a23e85f4e6aa22220775c774a97, auth error: 401, auth error code: invalid_api_key
译文:意外状态 401 未授权:提供的 API key 不正确:sk-ae9e7*******************************************************b6e6。您可以在 https://platform.openai.com/account/api-keys 找到您的 API key,url:https://api.openai.com/v1/responses,cf-ray:a31b927e0bd3fa49-SJC,请求 id:req_c0876a23e85f4e6aa22220775c774a97,认证错误:401,认证错误代码:invalid_api_key
2. 🎯 原因分析
https://api.openai.com/v1/responses 端点时,因 API key 无效返回 401 未授权错误3. 💡 解决方案
#### 步骤1:获取有效API Key
登录 OpenAI 平台,前往 API Keys 页面生成新的有效 API key。
#### 步骤2:更新代码中的API Key
将代码中的无效 API key 替换为新生成的有效 key,示例如下:
import openai
# 使用有效的API key替换下面的占位符
openai.api_key = "your-valid-api-key-here"
try:
response = openai.Response.create(
model="gpt-4o",
input="Hello, world!"
)
print(response)
except Exception as e:
print(f"Error: {e}")