{"record":{"id":"3fee94e4d628e20c","repo":"diegosouzapw/OmniRoute","slug":"openference-token-exchange-failed-error","errorCode":null,"errorMessage":"Openference token exchange failed: ${error}","messagePattern":"Openference token exchange failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/lib/oauth/providers/openference.ts","lineNumber":87,"sourceCode":"  exchangeToken: async (config, code, redirectUri, codeVerifier) => {\n    const response = await fetch(config.tokenUrl, {\n      method: \"POST\",\n      headers: {\n        \"Content-Type\": \"application/x-www-form-urlencoded\",\n        Accept: \"application/json\",\n      },\n      body: new URLSearchParams({\n        grant_type: \"authorization_code\",\n        client_id: config.clientId,\n        code,\n        redirect_uri: redirectUri,\n        code_verifier: codeVerifier,\n      }),\n    });\n\n    if (!response.ok) {\n      const error = await response.text();\n      throw new Error(`Openference token exchange failed: ${error}`);\n    }\n\n    return response.json();\n  },\n\n  postExchange: async (tokens) => {\n    const userinfoUrl = OPENFERENCE_CONFIG.userinfoUrl;\n    const headers = {\n      Authorization: `Bearer ${tokens.access_token}`,\n      Accept: \"application/json\",\n    };\n\n    const userRes = await fetch(userinfoUrl, { headers });\n    const userInfo = userRes.ok ? ((await userRes.json()) as Record<string, unknown>) : {};\n\n    return { userInfo };\n  },\n","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/diegosouzapw/OmniRoute/blob/a179ffed5bb2e0b883b9ae7214ce8717b2a94c4d/src/lib/oauth/providers/openference.ts#L69-L105","documentation":"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.","triggerScenarios":"Thrown at src/lib/oauth/providers/openference.ts:87 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Read the appended body for the OAuth error (invalid_grant, invalid_client, etc.)","Ensure redirectUri exactly matches the one used in the authorize request","Verify the PKCE code_verifier corresponds to the original code_challenge","Restart the authorization flow to mint a fresh code if it expired","Sanitize upstream text before returning in API responses"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"a179ffed5bb2e0b883b9ae7214ce8717b2a94c4d","analyzedAt":"2026-08-25T18:35:09.898Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}