Skip to content

Auto-refresh expired OAuth token instead of showing an HTTP error - #30

Open
dvdstelt wants to merge 1 commit into
Haletran:mainfrom
dvdstelt:feat/auto-refresh-oauth-token
Open

Auto-refresh expired OAuth token instead of showing an HTTP error#30
dvdstelt wants to merge 1 commit into
Haletran:mainfrom
dvdstelt:feat/auto-refresh-oauth-token

Conversation

@dvdstelt

@dvdstelt dvdstelt commented Jun 3, 2026

Copy link
Copy Markdown

Problem

The indicator reads claudeAiOauth.accessToken from .credentials.json and sends it as-is to the usage endpoint. OAuth access tokens are short-lived, so once the stored token expires the panel just shows HTTP 401 until the Claude Code CLI happens to run and rewrite the file. If you haven't used the CLI in a while, the indicator sits in an error state.

For me, this is an issue because I use Claude Desktop all the time instead of the CLI.

Fix

Refresh the token in-extension using the refreshToken that's already in the credentials file:

  • Check expiresAt before fetching and refresh proactively (60s buffer)
  • Also retry once on a 401 in case the expiry metadata is stale
  • On success, write the rotated accessToken / refreshToken / expiresAt back to .credentials.json (file mode kept at 0600) so the Claude Code CLI stays in sync on its next run

The token endpoint (https://console.anthropic.com/v1/oauth/token) and client_id match the Claude Code OAuth flow.

Notes for reviewer

  • This makes the extension write to .credentials.json, a file the CLI also owns. That's intentional (keeps both in sync via refresh-token rotation), but flagging it for your review.
  • The refresh endpoint and client_id are the documented Claude Code OAuth values, not an official public API — worth being aware of if Anthropic changes the flow.

The indicator read claudeAiOauth.accessToken from .credentials.json and
sent it as-is. OAuth access tokens are short-lived, so once the stored
token expired the panel showed an HTTP error (401) until the Claude Code
CLI happened to rewrite the file.

Refresh the token in-extension using the refreshToken: check expiresAt
before fetching and refresh proactively, and also retry once on a 401.
On success the rotated access/refresh tokens and new expiry are written
back to .credentials.json (mode kept at 0600) so the CLI stays in sync.

Token endpoint and client_id match the Claude Code OAuth flow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant