我写这个项目,是为了把 ChatGPT / Codex OAuth 凭证接到本地兼容 API 上,方便本地工具或客户端直接走统一接口。
当前我主要做了这些能力:
- 支持 OpenAI Codex / ChatGPT Plus/Pro OAuth 登录
- 支持 GitHub Copilot OAuth 登录
- 启动本地 HTTP 服务,转发到 ChatGPT Codex Responses 上游
- 对外暴露兼容接口:
POST /v1/chat/completionsPOST /v1/chat/responsesPOST /v1/messagesPOST /anthropic/v1/messages
- 提供一个简单前端页面,用来测试流式对话
.
├── frontend/ # 前端测试页面
├── oauth/ # OAuth 登录、刷新、凭证管理
├── main.go # CLI 入口 + 本地 API 代理服务
├── go.mod
└── go.sum
- Go 1.25+
- macOS / Linux / Windows
- 本机能打开浏览器完成 OAuth 登录
go.mod当前声明为go 1.25.0。
go build -o chatgpt-to-api .编译后运行:
./chatgpt-to-apigo run . server --port 8080入口命令:
chatgpt-to-api [command]chatgpt-to-api login --provider openai-codex
chatgpt-to-api login --provider github-copilot
chatgpt-to-api refresh --provider openai-codex
chatgpt-to-api logout --provider openai-codex
chatgpt-to-api relogin --provider openai-codex
chatgpt-to-api status
chatgpt-to-api server --port 8080当前支持两个 provider:
openai-codexgithub-copilot
默认 provider 是:
openai-codex
如果要用 ChatGPT / Codex 凭证:
./chatgpt-to-api login --provider openai-codex登录时会:
- 自动拉起浏览器
- 完成 OAuth 授权
- 把凭证保存到本地
默认凭证路径:
~/.config/chatgpt-to-api/openai-codex.json如果要用 GitHub Copilot:
./chatgpt-to-api login --provider github-copilot对应凭证路径:
~/.config/chatgpt-to-api/github-copilot.json./chatgpt-to-api status这里会检查:
- OpenAI Codex 是否已配置
- GitHub Copilot 是否已配置
./chatgpt-to-api server --port 8080启动后默认地址:
http://localhost:8080
健康检查:
curl http://localhost:8080/health返回示例:
{"status":"ok"}启动服务后,可用接口如下。
POST /v1/chat/completions
请求示例:
curl -N http://localhost:8080/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-5.4",
"stream": true,
"messages": [
{"role": "system", "content": "你是一个简洁的助手"},
{"role": "user", "content": "用三句话介绍 Go"}
]
}'POST /v1/chat/responses
POST /v1/messages
POST /anthropic/v1/messages
这两个接口主要是为了兼容 Claude / Anthropic 风格客户端。
我这里会把外部传入的模型名映射成 Codex 上游模型。
当前代码里能直接看到的常用模型包括:
gpt-5.4gpt-5.3-codexgpt-5.3-codex-sparkgpt-5.2-codexgpt-5.1-codexgpt-5.1-codex-max
如果本机存在 ~/.codex/models_cache.json,也会优先读取里面可用的模型。
项目里带了一个简单页面:
frontend/index.html
这个页面可以用来:
- 发送流式聊天请求
- 选择 provider
- 调用登录 / 重登 / 注销接口
- 查看流式输出效果
这是一个纯静态页面,可以直接用任意静态文件服务器打开。
例如在项目根目录执行:
python3 -m http.server 3000然后访问:
http://localhost:3000/frontend/index.html
页面默认 API 地址是:
http://localhost:8081/v1/chat/completions
如果后端实际跑在 8080,就在页面设置里改成:
http://localhost:8080/v1/chat/completions
服务里还留了一组认证管理接口:
GET /v1/auth/status
POST /v1/auth/login
请求示例:
{
"provider": "openai-codex"
}POST /v1/auth/relogin
POST /v1/auth/logout
POST /v1/auth/import
请求体示例:
{
"access_token": "your_access_token",
"refresh_token": "your_refresh_token"
}如果我要把这个服务当成 Claude / Anthropic 风格的本地网关来用,可以把 Claude 侧的 Base URL 指到这个服务。
因为这里提供了:
POST /v1/messagesPOST /anthropic/v1/messages
所以最直接的方式,就是把客户端请求改到本地服务,例如:
http://localhost:8080/anthropic/v1/messages
或:
http://localhost:8080/v1/messages
需要在 Claude 使用侧配置:
- Base URL 指向本地服务
- Messages API 路径使用这里暴露的接口
- 模型名可传
sonnet/opus/haiku,代码里会映射到合适的 Codex 模型
当前代码里的 Anthropic 模型映射规则大致是:
sonnet/claude-sonnet*-> 首选 Codex 模型opus/claude-opus*-> 更强的 Codex 模型haiku/claude-haiku*-> 更小的 Codex 模型
可以直接按 Anthropic Messages 格式发请求:
curl -N http://localhost:8080/anthropic/v1/messages \
-H 'Content-Type: application/json' \
-d '{
"model": "sonnet",
"stream": true,
"messages": [
{"role": "user", "content": "你好,介绍一下你自己"}
]
}'默认配置和凭证保存在:
~/.config/chatgpt-to-api/
常见文件包括:
openai-codex.jsongithub-copilot.jsonconfig.json
server模式启动时,会优先读取:~/.config/chatgpt-to-api/openai-codex.json- 如果没有,再尝试
~/.config/chatgpt-to-api/config.json
- 前端页面默认端口是
8081,而后端命令示例里常用8080,要注意保持一致。 - OAuth 登录依赖本机浏览器与本地回调端口:
- OpenAI Codex 默认回调地址:
http://localhost:1455/auth/callback
- OpenAI Codex 默认回调地址:
- 如果 access token 过期,程序会尝试自动 refresh。
# 1. 编译
go build -o chatgpt-to-api .
# 2. 登录
./chatgpt-to-api login --provider openai-codex
# 3. 启动服务
./chatgpt-to-api server --port 8080
# 4. 调用接口
curl -N http://localhost:8080/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-5.4",
"stream": true,
"messages": [
{"role": "user", "content": "你好"}
]
}'