jackwener/OpenCLI · error · AuthRequiredError
Failed to fetch collection data from Zhihu. Please ensure yo
Error message
Failed to fetch collection data from Zhihu. Please ensure you are logged in.
What it means
fetchCollectionPage throws AuthRequiredError when the in-page fetch of Zhihu's collection API returns data with __httpError equal to 401 or 403, which means Zhihu rejected the request as unauthenticated or forbidden. The CLI drives a logged-in browser page; these statuses indicate the login session is missing or expired, so the collection data cannot be fetched.
Source
Thrown at clis/zhihu/collection.js:35
throw new ArgumentError(`zhihu collection --${name} must be a non-negative integer`, 'Example: opencli zhihu collection 83283292 --offset 0');
}
return n;
}
async function fetchCollectionPage(page, collectionId, offset, limit) {
const url = `https://www.zhihu.com/api/v4/collections/${collectionId}/items?offset=${offset}&limit=${limit}`;
const data = await page.evaluate(`
(async () => {
const r = await fetch(${JSON.stringify(url)}, { credentials: 'include' });
if (!r.ok) return { __httpError: r.status };
return await r.json();
})()
`);
if (!data || data.__httpError) {
const status = data?.__httpError;
if (status === 401 || status === 403) {
throw new AuthRequiredError('www.zhihu.com', 'Failed to fetch collection data from Zhihu. Please ensure you are logged in.');
}
throw new CommandExecutionError(
status ? `Zhihu collection request failed (HTTP ${status})` : 'Zhihu collection request failed',
'Try again later or rerun with -v for more detail',
);
}
return data;
}
function itemKey(item) {
const content = item?.content || {};
return `${content.type || ''}:${content.id || content.url || JSON.stringify(content).slice(0, 80)}`;
}
function mapCollectionItem(item, rank) {
const content = item.content || {};
const type = content.type || '';
if (!['answer', 'article', 'pin'].includes(type)) {View on GitHub (pinned to 49907e53dc)
Solutions
- Log in to www.zhihu.com in the browser session the CLI uses, then rerun the command
- Clear cookies for zhihu.com and log in again to refresh an expired session
- Wait and retry later if 403 is caused by Zhihu anti-crawler rate limiting
- Rerun with -v to inspect the underlying HTTP response details
Example fix
// before await opencli zhihu collection 83283292 // after opencli auth login zhihu # or manually log in at zhihu.com in the CLI browser profile opencli zhihu collection 83283292
Defensive patterns
Strategy: try-catch
Validate before calling
const isNumericId = /^\d+$/.test(String(id));
if (!isNumericId) throw new Error('collection id must be numeric before calling zhihu collection'); Type guard
function hasHttpError(d) { return d != null && typeof d === 'object' && '__httpError' in d; }
function isAuthStatus(d) { return hasHttpError(d) && (d.__httpError === 401 || d.__httpError === 403); } Try / catch
try {
const rows = await zhihuCollection(id);
} catch (e) {
if (e.name === 'AuthRequiredError') { await loginToZhihu(); return retry(); }
throw e;
} Prevention
- Log in to zhihu.com in the CLI's browser profile before running collection commands
- Re-login periodically; Zhihu sessions expire
- Avoid rapid repeated calls that trip anti-crawler 403s
- Only fetch collections you have permission to view
When it happens
Trigger: The page.evaluate fetch to the Zhihu collection API returns HTTP 401 or 403: not logged in, session cookie expired, Zhihu anti-crawler block, or accessing a private collection without permission.
Common situations: Running the CLI without ever logging into zhihu.com in the managed browser profile; a stale session after Zhihu invalidated cookies; Zhihu rate-limiting/scraping protection returning 403; fetching another user's private collection.
Related errors
- Failed to fetch Zhihu answer detail
- ${errorLabel} from Zhihu failed. Please ensure you are logge
- Gmail ${operation} returned HTTP ${status}
- 未登录或登录已过期,请先在浏览器中登录贝壳找房
- LinkedIn Learning auth failed (HTTP ${result.status ?? ''}).
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/a3738f76a0b0b677.
Report an issue: GitHub.