[BUG] OAuth login fails with "Invalid code" in GitHub Codespaces — both browser-redirect and setup-token flows affected
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Login fails with an "Invalid code" OAuth error in a GitHub Codespace. Both the standard /login browser-redirect flow and the claude setup-token manual flow fail the same way. This Codespace has authenticated successfully many times before; this is a new, sudden failure with no config changes on my end.
What Should Happen?
Claude Code should authenticate successfully after completing the browser authorization step and returning the code to the terminal.
Error Messages/Logs
OAuth error: Invalid code. Please make sure the full code was copied
Press Enter to retry.
Esc to cancel
This site can't be reached
localhost refused to connect.
ERR_CONNECTION_REFUSED
Steps to Reproduce
- In the Codespace terminal, run
claude(orclaude setup-token). - A browser popup appears asking "Do you want Code to open the external website?" — this points to a short URL ending in a redirect. If opened, it redirects to localhost:PORT/callback, which fails with ERR_CONNECTION_REFUSED (localhost is not reachable from the browser in this environment).
- Instead, use the longer plain-text URL printed separately in the terminal output (starts https://claude.com/cai/oauth/authorize?code=true&client_id=...). Paste this into a browser tab manually.
- Complete the Anthropic authorization screen ("Claude Code would like to connect to your Claude chat account") and click Authorize.
- The browser redirect still fails (same ERR_CONNECTION_REFUSED on localhost), or in the setup-token flow, a code is expected to be pasted back into the terminal at "Paste code here if prompted >".
- Copy the code (and/or state parameter) from the resulting URL and paste it into the terminal.
- Terminal returns: "OAuth error: Invalid code. Please make sure the full code was copied" and offers "Press Enter to retry."
This was repeated multiple times with fresh codes, using: code alone, code+state concatenated with '#', and via both /login and setup-token flows. Same result every time.
Claude Model
None
Is this a regression?
Yes, this worked in a previous version
Last Working Version
Unknown exact version — this Codespace has authenticated successfully many times over several months prior to this session. First failure was today.
Claude Code Version
2.1.252 (Claude Code)
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
VS Code integrated terminal
Additional Information
- echo $ANTHROPIC_API_KEY returns empty — no alternate auth path available.
- Claude Code self-updated during troubleshooting (confirmed via "Update installed · Restart to update" message); version remained 2.1.252 after restart, and the issue persisted.
- The auto-launched popup dialog and the plain-text URL printed in the terminal appear to be two different URLs with different redirect targets — only the popup's URL attempts the localhost redirect that fails. This inconsistency may be relevant to the root cause.
- Screenshots of the exact terminal output and error state are available on request.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗