jackwener/OpenCLI · error · AuthRequiredError

xueqiu.com

Error message

xueqiu.com

What it means

fetchXueqiuJson fetches xueqiu JSON APIs from inside the logged-in browser context and inspects the response status. When the API returns HTTP 401 or 403 it maps that to AuthRequiredError('xueqiu.com', '未登录或登录已过期'), signaling that the browser session lacks valid xueqiu credentials (cookies). xueqiu requires a logged-in session with a valid xq_a_token cookie for most v5 API endpoints; anonymous or expired sessions get rejected with 400/401/403.

Source

Thrown at clis/xueqiu/utils.js:50

 * Fetch a xueqiu JSON API from inside the browser context (credentials included).
 * Page must already be navigated to xueqiu.com before calling this function.
 * Throws CliError on HTTP errors; otherwise returns the parsed JSON.
 */
export async function fetchXueqiuJson(page, url) {
    const result = await page.evaluate(`(async () => {
    const res = await fetch(${JSON.stringify(url)}, { credentials: 'include' });
    if (!res.ok) return { __xqErr: res.status };
    try {
      return await res.json();
    } catch {
      return { __xqErr: 'parse' };
    }
  })()`);
    const r = result;
    if (r?.__xqErr !== undefined) {
        const code = r.__xqErr;
        if (code === 401 || code === 403) {
            throw new AuthRequiredError('xueqiu.com', '未登录或登录已过期');
        }
        if (code === 'parse') {
            throw new CommandExecutionError('响应不是有效 JSON', '可能触发了风控,请检查登录状态或稍后重试');
        }
        throw new CommandExecutionError(`HTTP ${code}`, '请检查网络连接或登录状态');
    }
    return result;
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Open a xueqiu.com page in the CLI browser and log in manually to refresh the session cookies
  2. Re-run the command after confirming the browser session is logged in
  3. Slow down request frequency / wait if 403 was caused by rate limiting or risk control
  4. Clear browser state and re-authenticate if cookies are corrupted

Example fix

// before (page never authenticated)
await page.goto('https://xueqiu.com');
const d = await fetchXueqiuJson(page, url); // throws AuthRequiredError
// after (verify login before calling)
await page.goto('https://xueqiu.com');
const loggedIn = await page.evaluate(() => document.cookie.includes('xq_a_token'));
if (!loggedIn) throw new AuthRequiredError('xueqiu.com', '请先登录 xueqiu.com');
const d = await fetchXueqiuJson(page, url);
Defensive patterns

Strategy: try-catch

Validate before calling

const loggedIn = await page.evaluate(() => document.cookie.includes('xq_a_token'));
if (!loggedIn) throw new Error('Not logged into xueqiu.com');

Type guard

function isAuthRequiredError(e) { return e instanceof AuthRequiredError; }

Try / catch

try {
  const d = await fetchXueqiuJson(page, url);
} catch (e) {
  if (e instanceof AuthRequiredError) {
    // prompt user to log into xueqiu.com in the browser session
    return { error: 'AUTH_REQUIRED', hint: 'Login to xueqiu.com and retry' };
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling any xueqiu CLI command (e.g. watchlist, groups) when the browser page was never logged into xueqiu.com, or the session cookie (xq_a_token) has expired, or xueqiu risk control rejects the request with 401/403.

Common situations: Cookie expired after days of use; fresh browser profile never logged in; scraping too fast triggers anti-bot 403; xueqiu changed token requirements; running headless without persisted login state.

Related errors


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