{"record":{"id":"cc48a264f6bed650","repo":"jackwener/OpenCLI","slug":"zhihu-z-c0-cookie-missing-anonymous","errorCode":null,"errorMessage":"Zhihu z_c0 cookie missing — anonymous","messagePattern":"Zhihu z_c0 cookie missing — anonymous","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/zhihu/auth.js","lineNumber":11,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\n\nasync function hasZhihuAuthCookie(page) {\n  const cookies = await page.getCookies({ url: 'https://www.zhihu.com' });\n  return cookies.some(c => c.name === 'z_c0' && c.value);\n}\n\nasync function verifyZhihuIdentity(page) {\n  if (!await hasZhihuAuthCookie(page)) {\n    throw new AuthRequiredError('www.zhihu.com', 'Zhihu z_c0 cookie missing — anonymous');\n  }\n  await page.goto('https://www.zhihu.com/');\n  await page.wait(2);\n  const data = await page.evaluate(`\n    (async () => {\n      try {\n        const r = await fetch('https://www.zhihu.com/api/v4/me?include=url_token', { credentials: 'include' });\n        if (!r.ok) return { __httpError: r.status };\n        return await r.json();\n      } catch (e) {\n        return { __exception: String(e && e.message || e) };\n      }\n    })()\n  `);\n  if (data?.__exception) {\n    throw new CommandExecutionError(`Zhihu whoami failed: ${data.__exception}`);\n  }\n  if (!data || data.__httpError) {","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/auth.js#L1-L29","documentation":"verifyZhihuIdentity checks page cookies for a non-empty `z_c0` cookie, which is Zhihu's logged-in session token. If absent it throws AuthRequiredError, meaning the browser session is anonymous and identity cannot be verified. The library treats z_c0 as the prerequisite for any authenticated Zhihu operation.","triggerScenarios":"Calling any command that verifies identity (e.g. zhihu whoami, answer, collection) while the browser has never logged in, after logout, or when cookies were cleared so no z_c0 cookie exists for https://www.zhihu.com.","commonSituations":"Fresh browser profile with no login; cookie expired/purged by Zhihu; running headless with a new profile instead of the logged-in user data dir; the CLI reading cookies from the wrong URL scope.","solutions":["Run `opencli zhihu login` and complete the sign-in in the browser so a z_c0 cookie is set.","Persist and reuse the same browser profile/user-data-dir across runs so cookies survive.","Manually log into zhihu.com in the automated browser if QR/SMS login is required.","Catch AuthRequiredError and prompt the user to log in rather than retrying blindly."],"exampleFix":"// before\nawait cli.run('zhihu whoami'); // AuthRequiredError: z_c0 cookie missing\n// after\nawait cli.run('zhihu login');  // completes browser sign-in, sets z_c0\nawait cli.run('zhihu whoami');","handlingStrategy":"validation","validationCode":"const cookies = await page.getCookies({ url: 'https://www.zhihu.com' });\nconst loggedIn = cookies.some(c => c.name === 'z_c0' && c.value);\nif (!loggedIn) await runZhihuLogin();","typeGuard":"function hasZhihuAuthCookie(cookies) { return Array.isArray(cookies) && cookies.some(c => c.name === 'z_c0' && !!c.value); }","tryCatchPattern":"try { await verifyZhihuIdentity(page); } catch (e) { if (String(e.message).includes('z_c0 cookie missing')) { await zhihuLogin(); return verifyZhihuIdentity(page); } throw e; }","preventionTips":["Use a persistent user-data-dir so cookies survive restarts","Run zhihu login before any authenticated command","Check hasZhihuAuthCookie (quickCheck) before long batch jobs","Avoid clearing cookies between automation steps"],"tags":["authentication","zhihu","cookie","browser-session"],"backgroundTag":"auth-cookie-missing","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}