jackwener/OpenCLI · error · AuthRequiredError

bilibili.com

Error message

bilibili.com

What it means

getSelfUid fetches the nav API to determine the logged-in user's mid; if the response contains no data.mid, the caller is not authenticated, so the library throws AuthRequiredError('bilibili.com'). This is how commands like uid/self detect that valid cookies are missing.

Source

Thrown at clis/bilibili/utils.js:276

        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body: body.toString(),
      });
      // Bilibili write endpoints can return an HTML risk-control page (e.g. HTTP 412)
      // instead of JSON. Surface that as a structured error rather than a parse crash.
      const text = await res.text();
      try {
        return JSON.parse(text);
      } catch {
        return { code: -1, message: "Non-JSON response (HTTP " + res.status + "): " + text.slice(0, 200) };
      }
    }
  `);
}
export async function getSelfUid(page) {
    const nav = await getNavData(page);
    const mid = nav?.data?.mid;
    if (!mid)
        throw new AuthRequiredError('bilibili.com');
    return String(mid);
}
export async function resolveUid(page, input) {
    if (/^\d+$/.test(input))
        return input;
    // Search for user by name
    const payload = await apiGet(page, '/x/web-interface/wbi/search/type', {
        params: { search_type: 'bili_user', keyword: input },
        signed: true,
    });
    if (!payload || typeof payload !== 'object' || Array.isArray(payload) || !payload.data || typeof payload.data !== 'object' || Array.isArray(payload.data) || !Object.hasOwn(payload.data, 'result')) {
        throw new CommandExecutionError(`Bilibili user search returned malformed result for ${input}`);
    }
    const results = payload.data.result;
    if (!Array.isArray(results)) {
        throw new CommandExecutionError(`Bilibili user search returned malformed result for ${input}`);
    }
    if (results.length > 0) {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Log in to bilibili.com and export fresh cookies (SESSDATA at minimum) into your cookie file/env, then retry.
  2. Catch AuthRequiredError and run/instruct the login flow before calling commands that need identity.
  3. Confirm cookies were added to the page's browser context for domain .bilibili.com.
  4. If cookies look valid, hit https://api.bilibili.com/x/web-interface/nav manually and check data.isLogin/mid to debug.

Example fix

// before
const uid = await getSelfUid(page); // throws if not logged in
// after
let uid;
try {
  uid = await getSelfUid(page);
} catch (e) {
  if (e instanceof AuthRequiredError) {
    await loginFlow(page); // set fresh SESSDATA cookies
    uid = await getSelfUid(page);
  } else throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight auth check
const nav = await (await fetch('https://api.bilibili.com/x/web-interface/nav', { headers: { cookie } })).json();
if (!nav?.data?.mid) throw new Error('Not logged in: set SESSDATA cookie before calling uid/self');

Type guard

function isLoggedInNav(nav){ return !!nav?.data?.mid; }

Try / catch

try { const uid = await getSelfUid(page); } catch (e) { if (e instanceof AuthRequiredError) { await loginFlow(page); const uid = await getSelfUid(page); } else throw e; }

Prevention

When it happens

Trigger: Calling getSelfUid (directly or via the uid/self commands) without login cookies, with expired SESSDATA, or when the nav API returns isLogin:false so data.mid is undefined.

Common situations: Running in CI/containers without the cookie file; cookies expired after weeks; logged out in the browser you exported cookies from; Bilibili rotating SESSDATA invalidating old exports; wrong cookie domain passed to the page context.

Related errors


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