{"record":{"id":"3975435d6778254d","repo":"jackwener/OpenCLI","slug":"qwen-ai","errorCode":null,"errorMessage":"qwen.ai","messagePattern":"qwen\\.ai","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/qwen/auth.js","lineNumber":15,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\n\nasync function hasQwenSessionCookie(page) {\n  const cookies = await page.getCookies({ url: 'https://chat.qwen.ai' });\n  return cookies.some(c => c.name === 'token' && c.value);\n}\n\nasync function verifyQwenIdentity(page) {\n  // Source the token via CDP getCookies (works even if `token` is httpOnly,\n  // which document.cookie cannot read).\n  const cookies = await page.getCookies({ url: 'https://chat.qwen.ai' });\n  const token = cookies.find(c => c.name === 'token')?.value || '';\n  if (!token) {\n    throw new AuthRequiredError('qwen.ai', 'Qwen token cookie missing');\n  }\n  await page.goto('https://chat.qwen.ai/');\n  await page.wait(2);\n  const result = await page.evaluate(`(async () => {\n    try {\n      const token = ${JSON.stringify(token)};\n      const res = await fetch('/api/v1/auths/', { credentials: 'include', headers: { 'Authorization': 'Bearer ' + token, 'Accept': 'application/json' } });\n      if (res.status === 401 || res.status === 403) {\n        return { kind: 'auth', detail: 'Qwen /api/v1/auths/ 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: 'Qwen /api/v1/auths/ returned no user id' };\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":33,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/qwen/auth.js#L1-L33","documentation":"AuthRequiredError with site 'qwen.ai' thrown by verifyQwenIdentity when no cookie named 'token' is found among cookies for https://chat.qwen.ai (read via CDP getCookies so httpOnly cookies are visible). The library requires this token to probe the whoami API.","triggerScenarios":"Calling any qwen command that verifies identity while the browser profile has no qwen.ai session: never logged in, cookies cleared, or an expired session where the server dropped the token cookie.","commonSituations":"Fresh automation profile without login; cookie jar wiped by profile reset or incognito run; qwen.ai rotating/renaming the auth cookie after a site update.","solutions":["Log in to chat.qwen.ai in the automation browser profile, then retry the command.","Run the library's qwen auth/login command (registerSiteAuthCommands) to perform the flow.","Confirm via page.getCookies({url:'https://chat.qwen.ai'}) that a 'token' cookie exists before running commands.","If you did log in but the cookie is absent, check for a cookie-name change in the site and update the library."],"exampleFix":"// before\nawait askQwen(page, 'hello'); // fails: token cookie missing\n// after\nawait qwenLogin(page);        // establish session first\nawait askQwen(page, 'hello');","handlingStrategy":"validation","validationCode":"const cookies = await page.getCookies({ url: 'https://chat.qwen.ai' });\nif (!cookies.some(c => c.name === 'token' && c.value)) {\n  console.error('qwen.ai token cookie missing - login required');\n  process.exit(2);\n}","typeGuard":"function hasQwenToken(cookies) {\n  return Array.isArray(cookies) && cookies.some(c => c.name === 'token' && !!c.value);\n}","tryCatchPattern":"try {\n  await verifyQwenIdentity(page);\n} catch (e) {\n  if (e instanceof AuthRequiredError) {\n    await qwenLogin(page);\n    await verifyQwenIdentity(page);\n  } else { throw e; }\n}","preventionTips":["Use a persistent (non-incognito) browser profile with an active qwen.ai session","Verify the token cookie exists before batch runs","Re-login proactively when sessions age out","Watch for site cookie-name changes after qwen.ai updates"],"tags":["auth","cookies","session"],"backgroundTag":"auth-cookie-missing","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}