> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ruapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Codex 报「Token exchange failed」：为什么登不上

> 登录 Codex 时报「Token exchange failed」：「error sending request」和「403 Forbidden」有什么区别，要查什么，怎么用 API 密钥、不登 ChatGPT 也能用。

**先说结论**：`Token exchange failed` 出现在 ChatGPT 登录的最后一步：浏览器里已经登录了，但 Codex 自己没能从 `auth.openai.com` 拿到令牌。要么连不上这个地址，要么它回了 403。浏览器和 Codex 走的网络路径不一样，所以「浏览器都能打开」说明不了什么。可以照下面的步骤查网络和代理；也可以干脆不登录：用 RuAPI（独立的 API 网关，在俄罗斯能直接用）的密钥，Codex 不需要 ChatGPT 账号，也不用 VPN。

## 你看到的是哪一种

| 文字 | 意思 |
| - | - |
| `Token exchange failed: error sending request for url (https://auth.openai.com/oauth/token)` | Codex 连不上登录地址：网络、代理或证书 |
| `Token exchange failed: token endpoint returned status 403 Forbidden` | 连上了，但登录地址拒绝了。可能是代理或 VPN，也可能是请求来自的国家 |
| `unexpected status 403 Forbidden: …` | 这已经不是登录，是普通的模型请求被拒了。看冒号后面的文字，原因在那里 |

在应用里，同样的登录错误可能叫 `token_exchange_failed`。报错文字 2026-11-25 对照过 Codex 源码（[server.rs](https://github.com/openai/codex/blob/main/codex-rs/login/src/server.rs)、[oauth/error.rs](https://github.com/openai/codex/blob/main/codex-rs/login/src/oauth/error.rs)、[protocol/error.rs](https://github.com/openai/codex/blob/main/codex-rs/protocol/src/error.rs)）。

## 为什么会这样

* **令牌是 Codex 自己去拿的，不是浏览器。** 登录页在浏览器里打开，但最后一步用代码换令牌由 Codex 完成。代理只对浏览器生效的话，浏览器那步成功、Codex 报错。
* **留着旧的代理设置。** 代理换了端口或关了，`HTTP_PROXY` / `HTTPS_PROXY` 还指着旧地址，[GitHub 上的讨论](https://github.com/openai/codex/issues/26764)里就有人是这样。
* **代理拦了 localhost。** 登录完，浏览器会跳回你电脑上的本地地址，这个请求不能走代理：[另一个讨论](https://github.com/openai/codex/issues/2414)里有人把 localhost 排除后就好了。Codex 开发者在那里怀疑剩下的情况多半和代理、VPN 有关。
* **国家不支持。** OpenAI 不是每个国家都能用，API 的名单见 [Supported countries and territories](https://developers.openai.com/api/docs/supported-countries)。那页还提醒，从名单外的国家访问可能导致账号被封。

## 可以试的办法

**1. 更新 Codex**

`codex update` 装最新版（如果 Codex 是用 RuAPI 的一条命令装的，请[按这里](https://docs.ruapi.ai/zh/integrations/codex.md#常见问题)更新），`codex doctor` 检查安装、配置和登录。

**2. 去掉旧的代理设置**

看 `HTTP_PROXY`、`HTTPS_PROXY`、`ALL_PROXY` 变量和 `~/.codex/.env` 文件，删掉已经不能用的地址。

**3. localhost 不走代理**

加变量 `NO_PROXY=localhost,127.0.0.1,::1`，在新的终端窗口里重新登录。已经设过 `NO_PROXY` 的，不要覆盖，用逗号接在后面。

**4. 检查证书**

杀毒软件或公司网络会检查 HTTPS 的话，Codex 可能不信任被换掉的证书。登录时暂时关掉检查，或者看看 `SSL_CERT_FILE` 是不是指着过期的文件。

**5. 在终端里登录**

如果是应用里报错，在终端运行 `codex login`，再回到应用，[有人这样好了](https://github.com/openai/codex/issues/26764)。

## 不登 ChatGPT 也能用 Codex

Codex CLI 可以用 API 密钥接入，那就没有换令牌这一步。用 RuAPI 时请求发往 `https://www.ruapi.ai/v1`，不用 VPN，按 token 付费。

**1. 账号和密钥**

在 [www.ruapi.ai](https://www.ruapi.ai/?lng=zh-CN\&utm_source=docs\&utm_campaign=zh-codex-token-exchange-failed) 用邮箱、Google 或 Yandex 注册。密钥：控制台 →「**令牌管理**」→「**添加令牌**」。

**2. 余额**

控制台 →「**钱包管理**」：USDT 或 USDC，5 USDT 起。还没有 USDT 的话，[用 SBP 拿卢布买](https://docs.ruapi.ai/zh/buy-usdt.md)。外国银行的卡（Visa / Mastercard）也收。

**3. Codex**

用一条命令，或者手动改 `config.toml`，见 [Codex CLI](https://docs.ruapi.ai/zh/integrations/codex.md)。然后运行 `codex`，它不会再要你登录。

和 ChatGPT 订阅有什么区别，见[在俄罗斯用 Codex](https://www.ruapi.ai/codex-v-rossii?lng=zh-CN\&utm_source=docs\&utm_campaign=zh-codex-token-exchange-failed)。

## 常见问题

> [accordion] 浏览器里登录成功了，为什么 Codex 还报错？
>
> 因为令牌是 Codex 自己去拿的，它的网络设置可能和浏览器不一样。

> [accordion] 已经登录了，却报「unexpected status 403 Forbidden」
>
> 这是请求的错误，不是登录的错误。看冒号后面的文字，比如账号没有所选工作区的权限。如果 Codex 接的是 RuAPI，403 一般是余额不够，或密钥没开放这个模型，见[错误码](https://docs.ruapi.ai/zh/errors.md)。

> [accordion] 换个新的 ChatGPT 账号有用吗？
>
> 如果原因是网络、代理或国家，新账号会报同样的错。

> [accordion] 能用 OpenAI 的密钥登录 Codex 吗？
>
> 能，但要有充了钱的 OpenAI API 密钥，而且从你的地址能连上 OpenAI 的服务器。如果因为国家被拒，会报 [unsupported\_country\_region\_territory](https://docs.ruapi.ai/zh/openai-unsupported-country-region-territory.md)。

## 接下来

> [card] [Codex CLI 接入](https://docs.ruapi.ai/zh/integrations/codex.md)
>
> 一条命令，或改 config.toml。

> [card] [Codex：stream disconnected](https://docs.ruapi.ai/zh/codex-stream-disconnected.md)
>
> 回答中途断了。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.