jackwener/OpenCLI · error · AuthRequiredError

未登录或登录已过期,请先在浏览器中登录贝壳找房

Error message

未登录或登录已过期,请先在浏览器中登录贝壳找房

What it means

fetchKeJson performs a fetch with credentials from inside the browser page and inspects __keErr. When the endpoint responds 401 or 403 it throws AuthRequiredError meaning the Beike session is missing or expired — the server rejected the authenticated request, so the user must log into ke.com in the browser again.

Source

Thrown at clis/ke/utils.js:89

/**
 * Fetch a ke.com JSON API from inside the browser context (credentials included).
 */
export async function fetchKeJson(page, url) {
    const result = await page.evaluate(`(async () => {
    const res = await fetch(${JSON.stringify(url)}, { credentials: 'include' });
    if (!res.ok) return { __keErr: res.status };
    try {
      return await res.json();
    } catch {
      return { __keErr: 'parse' };
    }
  })()`);
    const r = result;
    if (r?.__keErr !== undefined) {
        const code = r.__keErr;
        if (code === 401 || code === 403) {
            throw new AuthRequiredError('ke.com', '未登录或登录已过期,请先在浏览器中登录贝壳找房');
        }
        if (code === 'parse') {
            throw new CommandExecutionError('响应不是有效 JSON', '可能触发了风控,请检查登录状态或稍后重试');
        }
        throw new CommandExecutionError(`HTTP ${code}`, '请检查网络连接或登录状态');
    }
    return result;
}

/**
 * Build a ke.com city URL prefix. Default city is 'bj' (Beijing).
 */
export function cityUrl(city) {
    return `https://${city}.ke.com`;
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-run the ke auth login flow to obtain a fresh lianjia_token
  2. Confirm the cookie via ke auth verify before the batch job
  3. If 403 persists with a valid cookie, the IP is likely flagged — switch network or slow down
  4. Check the exact endpoint in a logged-in browser to confirm it isn't permanently restricted

Example fix

// before
const data = await fetchKeJson(page, api); // throws on expired session
// after
if (!(await authVerify(page))) await authLogin(page); // ensure fresh session first
const data = await fetchKeJson(page, api);
Defensive patterns

Strategy: try-catch

Validate before calling

const ok = await page.evaluate(`fetch(${JSON.stringify(url)}, {credentials:'include', method:'HEAD'}).then(r => r.status)`);
if (ok === 401 || ok === 403) {
  console.log('Session expired — run `opencli ke auth login` before continuing');
  process.exit(2);
}

Try / catch

import { AuthRequiredError } from '@jackwener/opencli/errors';
try {
  const data = await fetchKeJson(page, apiUrl);
} catch (e) {
  if (e instanceof AuthRequiredError) {
    await keLogin(page);            // refresh session
    return fetchKeJson(page, apiUrl); // retry once
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling any ke.com JSON endpoint via fetchKeJson with an expired/invalid lianjia_token; cookies never set; server-side session revoked; endpoint requires auth the current anonymous session lacks.

Common situations: Sessions expire after hours/days; running scheduled jobs without re-login; token invalidated by logging in on another device; risk control returns 403 for flagged IPs even with a cookie.

Related errors


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