Token exchange failed: token endpoint returned status 403 Forbidden
Resolved 💬 42 comments Opened Aug 18, 2025 by mollon650 Closed Nov 18, 2025
💡 Likely answer: A maintainer (etraut-openai, contributor)
responded on this thread — see the highlighted reply below.
What version of Codex is running?
_No response_
Which model were you using?
_No response_
What platform is your computer?
_No response_
What steps can reproduce the bug?
What is the expected behavior?
_No response_
What do you see instead?
_No response_
Additional information
_No response_
42 Comments
Token exchange failed: token endpoint returned status 403 Forbidden
same problem
The codex CLI starts a local OAuth callback server on http://localhost:<port> inside WSL.
When you open the login link in a Windows browser, the browser attempts to redirect back to http://localhost:<port>.
Normally, Windows → WSL port forwarding for localhost works, so the callback should reach the server inside WSL. However, in some setups this fails due to:
Firewall or antivirus blocking the loopback traffic,
IPv6 vs IPv4 resolution issues (localhost vs 127.0.0.1),
Or because the login was performed entirely on the Windows side while the server is only listening inside WSL.
As a result, authentication cannot complete.
Solution:
In PowerShell on Windows, install and log in:
npm install -g @openai/codex
codex
Complete the login in the Windows browser.
After login, Windows stores credentials at:
C:\Users\<YourName>\.codex\auth.json
Copy this file into WSL:
mkdir -p ~/.codex
cp /mnt/c/Users/<YourName>/.codex/auth.json ~/.codex/auth.json
Now codex in WSL is authenticated without needing to repeat the browser flow.
$HIT,this works, either you are openai employee or you're a genius
<img width="1525" height="482" alt="Image" src="https://github.com/user-attachments/assets/058acb6d-6435-42bd-981e-e039db4019f4" />
设置localhost 与 127.0.0.1不经过代理就可以了
This worked for me^
``
brew install codex``didn^t work so i used the above commands.
you're a genius bro! thanks
Thanks for the clear write-up—super helpful. I ran into the same issue, and here’s what worked for me on macOS:
1. Install Codex CLI on macOS
2. Trigger the login and finish it in your macOS browser
3. Verify the credentials file exists
4. Copy the credentials to your remote Linux machine
After this, running codex on the remote host should work without repeating the browser OAuth flow.
thank you
Thanks @Hanminghao. Can I change the default location
~/.codexon the server?however, I can't login opanAI in macOS browser. when I login, browser return 'Token exchange failed: token endpoint returned status 403 Forbidden'. Is this related to my VPN?
I have resolved the issue. It was indeed caused by the VPN. My terminal wasn't using the VPN and required a manual proxy setup.
Bro, I am confused about the "terminal". Why your browser warning and you say the problem is in terminal?
老哥,这是在流程的哪一部分设置的?
I'm having this issue on Ubuntu 24.04, has anyone run into this before?
Try setting a proxy for your terminal and then run the codex authentication flow.
You can add the following helper function to your
.zshrcor.bashrc:Reload your shell config:
Enable proxy before running auth flow
(Yep, you can also pass a custom proxy address, e.g.
proxy set http://127.0.0.1:1080)Run the codex and go login, after finishing you can unset the proxy:
I'm able to login via copy and pasting the AUTH file over from Windows, but the conversation still could not be established with OpenAI server. The error message I'm getting after sending prompt is: "stream error: error sending request for url (https://chatgpt.com/backend-api/codex/responses)"
Thanks for the help! I tried the script but it returns error sending request message, maybe I did something wrong?
Thank you. It works For me.
---Original---
From: @.*>
Date: Fri, Sep 19, 2025 11:47 AM
To: @.*>;
Cc: "Yuanze @.****@.**>;
Subject: Re: [openai/codex] Token exchange failed: token endpoint returnedstatus 403 Forbidden (Issue #2414)
luckyJeffy left a comment (openai/codex#2414)
Try setting a proxy for your terminal and then run the codex authentication flow.
You can add the following helper function to your .zshrc or .bashrc:
proxy() { local cmd="$1" shift local default_addr="http://127.0.0.1:9527" local proxy_addr="${1:-$default_addr}" local ssh_config="$HOME/.ssh/config" case "$cmd" in set) export http_proxy="$proxy_addr" export https_proxy="$proxy_addr" export all_proxy="socks5://127.0.0.1:9527" git config --global http.proxy "$proxy_addr" git config --global https.proxy "$proxy_addr" mkdir -p "$HOME/.ssh" if ! grep -q "ProxyJump 127.0.0.1:9527" "$ssh_config" 2>/dev/null; then { echo "" echo "Host " echo " ProxyJump 127.0.0.1:9527" } >> "$ssh_config" fi ;; unset) unset http_proxy unset https_proxy unset all_proxy git config --global --unset http.proxy git config --global --unset https.proxy if [[ -f "$ssh_config" ]]; then sed -i '' '/ProxyJump 127.0.0.1:9627/d' "$ssh_config" sed -i '' '/Host \/d' "$ssh_config" fi ;; *) echo "Usage:" echo " proxy set [proxy_address]" echo " proxy unset" ;; esac }
Note: Git/SSH proxy is not required for codex login. I include them in my personal helper below—feel free to remove those lines if you don’t need them.
⚠️ Script only tested on macOS, sed -i '' syntax may differ on Linux
Reload your shell config:
source ~/.zshrc # or source ~/.bashrc
Enable proxy before running auth flow
proxy set
(Yep, you can also pass a custom proxy address, e.g. proxy set http://127.0.0.1:1080)
Run the codex and go login, after finishing you can unset the proxy:
proxy unset
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: @.***>
Hi @muye1202 , could you paste the full error message here so we can take a closer look?
Before running the script, please also verify that your proxy server is up and reachable, and that the address/port in the script matches your setup.
When on Windows:
FAIL mode
Using git bash to sign in fails with the same localhost:PORT/auth/bla
SUCCESS mode
Install codex through git bash, BUT _during the log in process_ use the regular Command Prompt:
codex -> Sign in with ChatGPT -> run though login -> Great Success!
Just bumped into the issue myself, tried a few times until realized that using windows standard tools would work better and it did.
Use this single sequence (Sign in with ChatGPT; no API key):
After testing it myself, when logging into Codex from VSCode, a localhost prompt as described above appears. Enabling the global proxy (TUN Mode) resolves the issue. The reason is that VSCode does not use the system proxy, so it cannot connect to openai.com, resulting in the inability to verify the returned token.
亲自测试,当从VSCode登录Codex时,出现localhost提示如上内容,开启全局代理(TUN Mode)即可解决。原因是VSCode不走系统代理,因此无法连接到openai.com,导致无法验证返回的token
I could login codex CLI and be authorized normally but failed in VSCode and returned "Token exchange failed: token endpoint returned status 403 Forbidden". Quite strange. My VPN is always TUN mode.
update: My codex in VSCode was suddenly authorized successfully when I left my PC for a while right after I send the comment above. Just so confusing. But maybe the problem is probably because VPN
nice
I ran into a similar problem. I wanted to log into Codex as an extension in VSCode. However, it turned out that due to the VPN, certain redirects didn’t work correctly (the sites opened, the requests were made, but I got a 403). For certain reasons, I couldn’t disable the VPN.
What helped was launching vscode like this:
fwiw, on MacOS the brew installation DID NOT work but the one with npm did work, prob cause npm has newer version.
At 2025-09-29:
npm install -g @openai/codexwould install version 0.42.0brew install codexwould install version 0.34.0太棒了,已经解决
Niubility
If you set proxy —> global model , maybe successful
I have the same problem as you, could you tell me how you configured the proxy manually?
love you
The TUN proxy method is applicable on Ubuntu, thank you.
If you are using codex from vscode in Windows, the problem can be fixed like this:
Firstly, open the system proxy and TUN mode in your proxy software like Clash. You can finish the sign-in like that.
Then, if you are good at using a proxy all the time, you can keep doing that, which is not preferred for most people. You can do the following:
Make a bat on your desktop:
Change the port and the location of your vscode.
Click the bat, your clash can turn off the system proxy and TUN mode. Use "test" to see if the codex is working
Everything is good.
Thanks a lot!!! MacOS26 & login codex on the remote server successfully
Ur genius
Since this bug report was filed, we've a number of bugs that were responsible for this behavior. I suspect that there are still some remaining conditions that can cause this — mostly involving network proxies or VPNs. I'm going to close this issue so we can focus our attention on the remaining issues. If you see a related problem, please open a fresh bug report and provide details and repro steps. In particular, include information about any network proxy, VPN or sandboxing environment that you're using.
How did you solve it? I'm also facing the same issues I'm using VPN due to China firewall restriction.
I've been trying to switch my Codex workflow from native Windows to running inside WSL, and I spent nearly four hours debugging it in the middle of the night, trying all kinds of methods.
Your solution finally worked — absolutely brilliant. Thank you so much!
te
This is absolutely genius
great solution!!!!
我在ubuntu里遇到这个问题,把vscode和网页的代理设置好就解决了