diegosouzapw/OmniRoute · error

xAI token exchange failed

Error message

xAI token exchange failed: ${error}

What it means

xAI's exchangeToken (authorization_code + PKCE) received a non-ok response from config.tokenUrl with the raw body appended. Indicates xAI's OAuth token endpoint rejected the exchange — invalid/expired code, code_verifier mismatch, redirect_uri mismatch, or upstream failure.

Solutions

  1. Read the appended error body for the standard OAuth error (invalid_grant, invalid_request...)
  2. Ensure the PKCE code_verifier matches the challenge sent at authorization
  3. Confirm the code is single-use and not expired; restart the flow if needed
  4. Retry with backoff on xAI 5xx; sanitize the message before exposing it
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/lib/oauth/providers/xai-oauth.ts:81 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of diegosouzapw/OmniRoute@a179ffed5b (2026-08-25). Data as JSON: /api/errors/23689645c5402982. Report an issue: GitHub.

Appendix: source

Thrown at src/lib/oauth/providers/xai-oauth.ts:81

  exchangeToken: async (config, code, redirectUri, codeVerifier) => {
    const response = await fetch(config.tokenUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        Accept: "application/json",
      },
      body: new URLSearchParams({
        grant_type: "authorization_code",
        client_id: config.clientId,
        code,
        redirect_uri: redirectUri,
        code_verifier: codeVerifier,
      }),
    });

    if (!response.ok) {
      const error = await response.text();
      throw new Error(`xAI token exchange failed: ${error}`);
    }

    return response.json();
  },

  mapTokens: (tokens) => {
    const identity = decodeXaiIdTokenIdentity(tokens.id_token);
    return {
      accessToken: tokens.access_token,
      refreshToken: tokens.refresh_token,
      idToken: tokens.id_token,
      expiresIn: tokens.expires_in,
      email: identity.email,
      name: identity.name || identity.email,
      providerSpecificData: {
        scope: tokens.scope || XAI_OAUTH_CONFIG.scope,
        tokenType: tokens.token_type || "Bearer",
      },

View on GitHub (pinned to a179ffed5b)