jackwener/OpenCLI · error · AuthRequiredError

${errorLabel} from Zhihu failed. Please ensure you are logge

Error message

${errorLabel} from Zhihu failed. Please ensure you are logged in.

What it means

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.

Source

Thrown at clis/zhihu/collections.js:25

  if (!Number.isInteger(n) || n <= 0) {
    throw new ArgumentError(`zhihu collections --${name} must be a positive integer`, 'Example: opencli zhihu collections --limit 20');
  }
  return n;
}

async function fetchJson(page, url, errorLabel) {
  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', `${errorLabel} from Zhihu failed. Please ensure you are logged in.`);
    }
    throw new CommandExecutionError(
      status ? `${errorLabel} from Zhihu failed (HTTP ${status})` : `${errorLabel} from Zhihu failed`,
      'Try again later or rerun with -v for more detail',
    );
  }
  return data;
}

function collectionKey(item) {
  return String(item?.id || item?.url || item?.title || '');
}

cli({
  site: 'zhihu',
  name: 'collections',
    access: 'read',
  description: '知乎收藏夹列表(需要登录)',

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Log in to www.zhihu.com in the CLI's browser profile and rerun
  2. Refresh the session (clear zhihu.com cookies, log in again)
  3. Slow down request rate if 403 stems from anti-crawler throttling
  4. Rerun with -v to see which endpoint and status triggered it

Example fix

// before
opencli zhihu collections   // 401: not logged in
// after
opencli auth login zhihu && opencli zhihu collections
Defensive patterns

Strategy: try-catch

Validate before calling

const sessionOk = await page.evaluate(() => document.cookie.includes('z_c0'));
if (!sessionOk) throw new Error('not logged in to zhihu.com; log in before calling collections');

Type guard

function isAuthFailure(d) { const s = d?.__httpError; return s === 401 || s === 403; }

Try / catch

try {
  return await zhihuCollections();
} catch (e) {
  if (e.name === 'AuthRequiredError') { await loginToZhihu(); return retryWithBackoff(zhihuCollections, 2); }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/e910bd8a2dba91f7. Report an issue: GitHub.