jackwener/OpenCLI · error · AuthRequiredError
Not logged into Xiaohongshu (could not resolve current user
Error message
Not logged into Xiaohongshu (could not resolve current user id)
What it means
Thrown by resolveXhsUserId when the browser-side evaluation of window.__INITIAL_STATE__.user.userInfo (or _value) yields no user_id/userId/userID string. It means the XHS web session does not expose a logged-in user id, so collection scraping cannot proceed.
Source
Thrown at clis/xiaohongshu/collection-helpers.js:226
if (Array.isArray(reqs) && reqs.length > 0)
bucket.push(...reqs);
return extractNotesFromResponses(bucket, fallbackUserId);
}
export async function resolveXhsUserId(page, rawId) {
if (rawId)
return normalizeXhsUserId(String(rawId));
await page.goto('https://www.xiaohongshu.com/explore');
await page.wait(2);
await throwIfLoginWall(page);
const userId = unwrapBrowserResult(await page.evaluate(`() => {
const user = window.__INITIAL_STATE__?.user?.userInfo;
const info = user?._value ?? user ?? {};
return info.user_id || info.userId || info.userID || '';
}`));
const clean = toCleanString(userId);
if (!clean) {
throw new AuthRequiredError('www.xiaohongshu.com', 'Not logged into Xiaohongshu (could not resolve current user id)');
}
return clean;
}
export async function extractNotesFromDom(page) {
const payload = unwrapBrowserResult(await page.evaluate(EXTRACT_COLLECTION_DOM_JS));
if (!Array.isArray(payload)) {
throw new CommandExecutionError('xiaohongshu collection DOM extraction returned malformed rows');
}
return payload.filter((item) => item?.id);
}
export async function fetchXhsCollectionNotes(page, {
userId,
profileTab,
apiPattern,
limit,
emptyLabel,View on GitHub (pinned to 49907e53dc)
Solutions
- Log into www.xiaohongshu.com in the browser profile/context used by the library (or supply cookies) and retry.
- Verify window.__INITIAL_STATE__?.user?.userInfo resolves in the page console before running the command.
- Check for XHS frontend changes and update the extraction script inside collection-helpers.js to the new state path.
- Wrap the call in try/catch for AuthRequiredError and prompt the user to re-authenticate.
Example fix
// before
const clean = toCleanString(userId);
if (!clean) throw new AuthRequiredError('www.xiaohongshu.com', 'Not logged into Xiaohongshu ...');
// after
const clean = toCleanString(userId);
if (!clean) {
await promptLogin(page); // guide user through login, then re-resolve
userId = await page.evaluate(...);
} Defensive patterns
Strategy: try-catch
Validate before calling
const userId = await page.evaluate('(() => { const u = window.__INITIAL_STATE__?.user?.userInfo; const i = u?._value ?? u ?? {}; return i.user_id || i.userId || i.userID || ""; })()');
if (!userId) throw new Error('Pre-check: not logged into XHS'); Type guard
function hasXhsUser(state) {
const u = state?.user?.userInfo;
const i = u?._value ?? u ?? {};
return typeof (i.user_id || i.userId || i.userID) === 'string' && Boolean(i.user_id || i.userId || i.userID);
} Try / catch
try {
const userId = await resolveXhsUserId(page);
} catch (e) {
if (e instanceof AuthRequiredError) {
await promptManualLogin(e.domain);
return retry();
}
throw e;
} Prevention
- Persist a logged-in browser profile (user data dir / cookies) between runs.
- Check login state on the profile page before starting collection scraping.
- Watch for XHS frontend changes to __INITIAL_STATE__ and update selectors.
- Handle AuthRequiredError explicitly instead of letting it crash batch jobs.
When it happens
Trigger: page.evaluate returns an empty string after toCleanString because the user info object is missing, the state has not hydrated, or the visitor is not authenticated.
Common situations: Expired or absent login cookies, scraping a fresh incognito browser context, XHS changed the __INITIAL_STATE__ shape, or the profile page redirected to a guest view.
Related errors
- Failed to fetch Barchart greeks for ${symbol}
- Failed to extract Booking.com cards: ${err?.message || err}
- Booking.com page returned no extractable data
- Booking.com extractor returned an invalid status
- Booking.com extractor returned malformed items
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/8e3125e1e96bf1d7.
Report an issue: GitHub.