lanmojun
lanmojun
发布于 2025-10-18 / 3 阅读 / 0 评论 / 0 点赞

WSL下安装codex提示Token exchange failed: token endpoint returned status 403 Forbidden

WSL下安装codex提示Token exchange failed: token endpoint returned status 403 Forbidden

本来是直接在命令行执行codex,结果输入中文和显示中文都会遇到很多问题,然后看到官方推荐使用WSL来运行codex,于是尝试使用WSL。安装步骤都非常顺利,但是执行codex后,在网页登录提示Token exchange failed: token endpoint returned status 403 Forbidden,问了GPT,说是网上很多人提到这个问题,给了解决方案是复用Windows下的凭证,登录问题解决:

# 1) 备份并移除 WSL 里的 ~/.codex
[ -d ~/.codex ] && mv ~/.codex ~/.codex.bak

# 2) 用你的 Windows 用户名替换 <WINUSER>
ln -s /mnt/c/Users/<WINUSER>/.codex ~/.codex

但是又遇到问题,进去codex后,任何对话都没有反应,最后提示错误,连接不上openai,很明显是网络问题了。但是我本机是开着梯子的,说明WSL没走代理。继续问GPT,给了WSL连接代理的方法(我之前一直以为WSL可以连接127.0.0.1的代理地址,结果不是):

1、首先在Windows的命令行下面执行 ipconfig ,获取所有IP地址,发给gpt,他会告诉你WSL走的是哪个IP

2、测试让WSL走这个代理,是否能连上openai:

在 WSL 终端执行以下命令:

export http_proxy="http://172.26.64.1:4780"
export https_proxy="$http_proxy"
export HTTP_PROXY="$http_proxy"
export HTTPS_PROXY="$http_proxy"
export NO_PROXY="localhost,127.0.0.1,::1,.local,172.26.64.1"

然后测试是否通:

curl -I --proxy "$http_proxy" https://api.openai.com

如果返回 HTTP/2 200 或 401/403,说明代理已经打通 ✅

3、我测试通过,于是继续问GPT这么永久让WSL走代理,直接给出代码,测试OK,终于可以正常的用codex了:

把刚才设置的环境变量写入 ~/.bashrc

cat <<'EOF' >> ~/.bashrc
# 自动设置 Windows 代理(适用于 WSL2)
export http_proxy="http://172.26.64.1:4780"
export https_proxy="$http_proxy"
export HTTP_PROXY="$http_proxy"
export HTTPS_PROXY="$http_proxy"
export NO_PROXY="localhost,127.0.0.1,::1,.local,172.26.64.1"
EOF

然后执行:

source ~/.bashrc

之后每次打开 WSL 都会自动走 Windows 的代理,不需要再手动设置。


评论