diegosouzapw/OmniRoute · error

Openference token exchange failed

Error message

Openference token exchange failed: ${error}

What it means

Openference's exchangeToken (authorization_code + PKCE grant) got a non-ok response from config.tokenUrl; the raw body is appended. Means the authorization code was rejected — expired/already-used code, wrong redirect_uri, bad code_verifier, or upstream token endpoint failure.

Solutions

  1. Read the appended body for the OAuth error (invalid_grant, invalid_client, etc.)
  2. Ensure redirectUri exactly matches the one used in the authorize request
  3. Verify the PKCE code_verifier corresponds to the original code_challenge
  4. Restart the authorization flow to mint a fresh code if it expired
  5. Sanitize upstream text before returning in API responses
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/lib/oauth/providers/openference.ts:87 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/3fee94e4d628e20c. Report an issue: GitHub.

Appendix: source

Thrown at src/lib/oauth/providers/openference.ts:87

  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(`Openference token exchange failed: ${error}`);
    }

    return response.json();
  },

  postExchange: async (tokens) => {
    const userinfoUrl = OPENFERENCE_CONFIG.userinfoUrl;
    const headers = {
      Authorization: `Bearer ${tokens.access_token}`,
      Accept: "application/json",
    };

    const userRes = await fetch(userinfoUrl, { headers });
    const userInfo = userRes.ok ? ((await userRes.json()) as Record<string, unknown>) : {};

    return { userInfo };
  },

View on GitHub (pinned to a179ffed5b)