{"record":{"id":"a3738f76a0b0b677","repo":"jackwener/OpenCLI","slug":"failed-to-fetch-collection-data-from-zhihu-please","errorCode":null,"errorMessage":"Failed to fetch collection data from Zhihu. Please ensure you are logged in.","messagePattern":"Failed to fetch collection data from Zhihu\\. Please ensure you are logged in\\.","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":401,"severity":"error","filePath":"clis/zhihu/collection.js","lineNumber":35,"sourceCode":"    throw new ArgumentError(`zhihu collection --${name} must be a non-negative integer`, 'Example: opencli zhihu collection 83283292 --offset 0');\n  }\n  return n;\n}\n\nasync function fetchCollectionPage(page, collectionId, offset, limit) {\n  const url = `https://www.zhihu.com/api/v4/collections/${collectionId}/items?offset=${offset}&limit=${limit}`;\n  const data = await page.evaluate(`\n    (async () => {\n      const r = await fetch(${JSON.stringify(url)}, { credentials: 'include' });\n      if (!r.ok) return { __httpError: r.status };\n      return await r.json();\n    })()\n  `);\n\n  if (!data || data.__httpError) {\n    const status = data?.__httpError;\n    if (status === 401 || status === 403) {\n      throw new AuthRequiredError('www.zhihu.com', 'Failed to fetch collection data from Zhihu. Please ensure you are logged in.');\n    }\n    throw new CommandExecutionError(\n      status ? `Zhihu collection request failed (HTTP ${status})` : 'Zhihu collection request failed',\n      'Try again later or rerun with -v for more detail',\n    );\n  }\n  return data;\n}\n\nfunction itemKey(item) {\n  const content = item?.content || {};\n  return `${content.type || ''}:${content.id || content.url || JSON.stringify(content).slice(0, 80)}`;\n}\n\nfunction mapCollectionItem(item, rank) {\n  const content = item.content || {};\n  const type = content.type || '';\n  if (!['answer', 'article', 'pin'].includes(type)) {","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/collection.js#L17-L53","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nawait opencli zhihu collection 83283292\n// after\nopencli auth login zhihu   # or manually log in at zhihu.com in the CLI browser profile\nopencli zhihu collection 83283292","handlingStrategy":"try-catch","validationCode":"const isNumericId = /^\\d+$/.test(String(id));\nif (!isNumericId) throw new Error('collection id must be numeric before calling zhihu collection');","typeGuard":"function hasHttpError(d) { return d != null && typeof d === 'object' && '__httpError' in d; }\nfunction isAuthStatus(d) { return hasHttpError(d) && (d.__httpError === 401 || d.__httpError === 403); }","tryCatchPattern":"try {\n  const rows = await zhihuCollection(id);\n} catch (e) {\n  if (e.name === 'AuthRequiredError') { await loginToZhihu(); return retry(); }\n  throw e;\n}","preventionTips":["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"],"tags":["auth","http-401","http-403","zhihu","network"],"backgroundTag":"auth-required-login-expired","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}