{"record":{"id":"e910bd8a2dba91f7","repo":"jackwener/OpenCLI","slug":"errorlabel-from-zhihu-failed-please-ensure-you","errorCode":null,"errorMessage":"${errorLabel} from Zhihu failed. Please ensure you are logged in.","messagePattern":"(.+?) from Zhihu failed\\. Please ensure you are logged in\\.","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":401,"severity":"error","filePath":"clis/zhihu/collections.js","lineNumber":25,"sourceCode":"  if (!Number.isInteger(n) || n <= 0) {\n    throw new ArgumentError(`zhihu collections --${name} must be a positive integer`, 'Example: opencli zhihu collections --limit 20');\n  }\n  return n;\n}\n\nasync function fetchJson(page, url, errorLabel) {\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', `${errorLabel} from Zhihu failed. Please ensure you are logged in.`);\n    }\n    throw new CommandExecutionError(\n      status ? `${errorLabel} from Zhihu failed (HTTP ${status})` : `${errorLabel} from Zhihu failed`,\n      'Try again later or rerun with -v for more detail',\n    );\n  }\n  return data;\n}\n\nfunction collectionKey(item) {\n  return String(item?.id || item?.url || item?.title || '');\n}\n\ncli({\n  site: 'zhihu',\n  name: 'collections',\n    access: 'read',\n  description: '知乎收藏夹列表（需要登录）',","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/collections.js#L7-L43","documentation":"fetchJson throws AuthRequiredError when an in-page fetch to a Zhihu API endpoint (any errorLabel, e.g. 'Zhihu user info request') returns HTTP 401 or 403, meaning the session is not authenticated or is forbidden. All collections-listing endpoints funnel through fetchJson, so any of them can raise this.","triggerScenarios":"Any fetchJson call (me endpoint or collections API) receives __httpError 401/403: no login, expired cookies, or Zhihu anti-crawler 403 on api/v4 endpoints.","commonSituations":"Running the command before ever logging in; session invalidated by Zhihu after inactivity; heavy repeated scraping triggering 403 blocks; using a region/IP flagged by Zhihu.","solutions":["Log in to www.zhihu.com in the CLI's browser profile and rerun","Refresh the session (clear zhihu.com cookies, log in again)","Slow down request rate if 403 stems from anti-crawler throttling","Rerun with -v to see which endpoint and status triggered it"],"exampleFix":"// before\nopencli zhihu collections   // 401: not logged in\n// after\nopencli auth login zhihu && opencli zhihu collections","handlingStrategy":"try-catch","validationCode":"const sessionOk = await page.evaluate(() => document.cookie.includes('z_c0'));\nif (!sessionOk) throw new Error('not logged in to zhihu.com; log in before calling collections');","typeGuard":"function isAuthFailure(d) { const s = d?.__httpError; return s === 401 || s === 403; }","tryCatchPattern":"try {\n  return await zhihuCollections();\n} catch (e) {\n  if (e.name === 'AuthRequiredError') { await loginToZhihu(); return retryWithBackoff(zhihuCollections, 2); }\n  throw e;\n}","preventionTips":["Check for the z_c0 session cookie before running","Refresh login when sessions go stale","Throttle requests to avoid anti-crawler 403s","Catch AuthRequiredError distinctly from CommandExecutionError"],"tags":["auth","http-401","http-403","zhihu"],"backgroundTag":"auth-required-login-expired","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}