{"record":{"id":"76abf11988db7164","repo":"jackwener/OpenCLI","slug":"kimi-access-token-cookie-missing","errorCode":null,"errorMessage":"Kimi access_token cookie missing","messagePattern":"Kimi access_token cookie missing","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/kimi/auth.js","lineNumber":16,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\n\nasync function hasKimiSessionCookie(page) {\n  const cookies = await page.getCookies({ url: 'https://www.kimi.com' });\n  const names = new Set(cookies.map(c => c.name));\n  return names.has('access_token') || names.has('refresh_token');\n}\n\nasync function verifyKimiIdentity(page) {\n  // Source the token via CDP getCookies (works even if access_token is httpOnly,\n  // which document.cookie cannot read).\n  const cookies = await page.getCookies({ url: 'https://www.kimi.com' });\n  const token = cookies.find(c => c.name === 'access_token')?.value || '';\n  if (!token) {\n    throw new AuthRequiredError('kimi.com', 'Kimi access_token cookie missing');\n  }\n  await page.goto('https://www.kimi.com/');\n  await page.wait(3);\n  const result = await page.evaluate(`(async () => {\n    try {\n      const token = ${JSON.stringify(token)};\n      const res = await fetch('/api/user', { credentials: 'include', headers: { 'Authorization': 'Bearer ' + token, 'Accept': 'application/json' } });\n      if (res.status === 401 || res.status === 403) {\n        return { kind: 'auth', detail: 'Kimi /api/user HTTP ' + res.status };\n      }\n      if (!res.ok) return { kind: 'http', httpStatus: res.status };\n      const d = await res.json();\n      if (!d || !d.id) {\n        return { kind: 'auth', detail: 'Kimi /api/user returned no id — anonymous' };\n      }\n      return { ok: true, user_id: String(d.id), name: String(d.name || '') };\n    } catch (e) {\n      return { kind: 'exception', detail: String(e && e.message || e) };","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/kimi/auth.js#L1-L34","documentation":"verifyKimiIdentity reads cookies via CDP page.getCookies for https://www.kimi.com and looks for the `access_token` cookie, which is the session credential for Kimi's API (this works even though the cookie is httpOnly). If no such cookie exists, it throws AuthRequiredError: there is no logged-in Kimi session to authenticate API calls with.","triggerScenarios":"Calling any kimi command (or the auth verify/quick check flow) while the browser profile has no `access_token` cookie for kimi.com — i.e. never logged in, logged out, or cookies were cleared.","commonSituations":"Fresh browser profile used before running `kimi login`; cookies wiped by clearing browsing data or using an incognito/temporary profile; Kimi renamed or expired the cookie; a proxy or region block prevented the login page from setting the cookie.","solutions":["Run the Kimi login flow (registerSiteAuthCommands `login` for site `kimi`) and complete the login in the browser window","Confirm the cookie exists: check `access_token` for https://www.kimi.com via devtools Application > Cookies","Use a persistent browser profile so cookies survive between runs","Re-run after logging in again if Kimi invalidated the old session"],"exampleFix":"// before\nawait runKimiCommand('whoami'); // throws: access_token cookie missing\n// after\nif (!await hasKimiSessionCookie(page)) {\n  await runKimiCommand('login'); // interactive login first\n}\nawait runKimiCommand('whoami');","handlingStrategy":"validation","validationCode":"const cookies = await page.getCookies({ url: 'https://www.kimi.com' });\nif (!cookies.some(c => c.name === 'access_token' && c.value)) {\n  throw new Error('Run `kimi login` first — no access_token cookie');\n}","typeGuard":"function hasKimiToken(cookies) {\n  return Array.isArray(cookies) && cookies.some(\n    c => c && c.name === 'access_token' && typeof c.value === 'string' && c.value.length > 0\n  );\n}","tryCatchPattern":"try {\n  await kimiWhoami();\n} catch (e) {\n  if (e instanceof AuthRequiredError || /access_token cookie missing/.test(e.message)) {\n    await kimiLogin(); // interactive login, then retry\n    await kimiWhoami();\n  } else throw e;\n}","preventionTips":["Run the login flow once per browser profile before scripted commands","Use a persistent profile directory so cookies survive restarts","Avoid incognito/temporary contexts for authenticated Kimi work"],"tags":["kimi","auth","cookie","login-required"],"backgroundTag":"missing-auth-cookie","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}