jackwener/OpenCLI · error · AuthRequiredError

Failed to fetch Zhihu answer detail

Error message

Failed to fetch Zhihu answer detail

What it means

This AuthRequiredError is thrown when the Zhihu answer detail request reports HTTP 401 or 403, meaning the request was rejected due to missing or insufficient authentication. The library surfaces this as an auth error for www.zhihu.com rather than a generic failure, because the API requires a valid logged-in session/cookies for this endpoint.

Source

Thrown at clis/zhihu/answer-detail.js:94

      (async () => {
        const r = await fetch(${JSON.stringify(apiUrl)}, { credentials: 'include' });
        if (!r.ok) return { __httpError: r.status };
        try {
          return await r.json();
        } catch (error) {
          return { __malformedJson: error instanceof Error ? error.message : String(error) };
        }
      })()
    `).catch((err) => {
            throw new CommandExecutionError(
                `Zhihu answer detail request failed: ${err instanceof Error ? err.message : String(err)}`,
                'Try again later or rerun with -v for more detail.',
            );
        });
        if (!data || data.__httpError) {
            const status = data?.__httpError;
            if (status === 401 || status === 403) {
                throw new AuthRequiredError('www.zhihu.com', 'Failed to fetch Zhihu answer detail');
            }
            if (status === 404) {
                throw new EmptyResultError('zhihu answer-detail', `No Zhihu answer was found for ${answerId}.`);
            }
            throw new CommandExecutionError(
                status
                    ? `Zhihu answer detail request failed (HTTP ${status})`
                    : 'Zhihu answer detail request failed',
                'Try again later or rerun with -v for more detail',
            );
        }
        if (data.__malformedJson) {
            throw new CommandExecutionError(
                `Zhihu answer detail returned malformed JSON: ${data.__malformedJson}`,
                'Try again later or rerun with -v for more detail',
            );
        }
        if (typeof data !== 'object' || Array.isArray(data)) {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Log into www.zhihu.com in the browser profile the CLI uses, then rerun the command.
  2. Refresh/renew Zhihu session cookies if they expired.
  3. If 403 persists, slow down request rate or solve the anti-bot challenge manually in the browser first.
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure a session exists before running
const cookies = await page.cookies('https://www.zhihu.com');
if (!cookies.some(c => c.name === 'z_c0')) throw new Error('Not logged into zhihu.com — auth required');

Type guard

function isAuthRequiredError(err) { return err instanceof AuthRequiredError || /AuthRequired/.test(err?.name || ''); }

Try / catch

try {
  const detail = await answerDetail(id);
} catch (err) {
  if (err instanceof AuthRequiredError) {
    console.error('Login to www.zhihu.com in the browser profile, then rerun.');
    process.exitCode = 3;
    return;
  }
  throw err;
}

Prevention

When it happens

Trigger: The in-page fetch returns __httpError === 401 or 403 from the Zhihu answer detail API — the browser profile has no zhihu.com session, cookies expired, or Zhihu's anti-bot gate rejected the request.

Common situations: Using a fresh browser profile that never logged into Zhihu, session cookies expired after weeks, Zhihu invalidated the session server-side, or an aggressive anti-crawler challenge returned 403.

Related errors


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