jackwener/OpenCLI · error · AuthRequiredError
Zhihu z_c0 cookie missing — anonymous
Error message
Zhihu z_c0 cookie missing — anonymous
What it means
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.
Source
Thrown at clis/zhihu/auth.js:11
import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { registerSiteAuthCommands } from '../_shared/site-auth.js';
async function hasZhihuAuthCookie(page) {
const cookies = await page.getCookies({ url: 'https://www.zhihu.com' });
return cookies.some(c => c.name === 'z_c0' && c.value);
}
async function verifyZhihuIdentity(page) {
if (!await hasZhihuAuthCookie(page)) {
throw new AuthRequiredError('www.zhihu.com', 'Zhihu z_c0 cookie missing — anonymous');
}
await page.goto('https://www.zhihu.com/');
await page.wait(2);
const data = await page.evaluate(`
(async () => {
try {
const r = await fetch('https://www.zhihu.com/api/v4/me?include=url_token', { credentials: 'include' });
if (!r.ok) return { __httpError: r.status };
return await r.json();
} catch (e) {
return { __exception: String(e && e.message || e) };
}
})()
`);
if (data?.__exception) {
throw new CommandExecutionError(`Zhihu whoami failed: ${data.__exception}`);
}
if (!data || data.__httpError) {View on GitHub (pinned to 49907e53dc)
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.
Example fix
// before
await cli.run('zhihu whoami'); // AuthRequiredError: z_c0 cookie missing
// after
await cli.run('zhihu login'); // completes browser sign-in, sets z_c0
await cli.run('zhihu whoami'); Defensive patterns
Strategy: validation
Validate before calling
const cookies = await page.getCookies({ url: 'https://www.zhihu.com' });
const loggedIn = cookies.some(c => c.name === 'z_c0' && c.value);
if (!loggedIn) await runZhihuLogin(); Type guard
function hasZhihuAuthCookie(cookies) { return Array.isArray(cookies) && cookies.some(c => c.name === 'z_c0' && !!c.value); } Try / catch
try { await verifyZhihuIdentity(page); } catch (e) { if (String(e.message).includes('z_c0 cookie missing')) { await zhihuLogin(); return verifyZhihuIdentity(page); } throw e; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Browser session required for zhihu answer
- Waiting for Zhihu z_c0 cookie
- Band band_session cookie missing
- Bilibili creator-center login is required: ${message}
- Browser session required for bilibili subtitle
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/cc48a264f6bed650.
Report an issue: GitHub.