{"record":{"id":"133e47df7d26eea3","repo":"jackwener/OpenCLI","slug":"bilibili-com","errorCode":null,"errorMessage":"bilibili.com","messagePattern":"bilibili\\.com","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/bilibili/utils.js","lineNumber":276,"sourceCode":"        headers: { \"Content-Type\": \"application/x-www-form-urlencoded\" },\n        body: body.toString(),\n      });\n      // Bilibili write endpoints can return an HTML risk-control page (e.g. HTTP 412)\n      // instead of JSON. Surface that as a structured error rather than a parse crash.\n      const text = await res.text();\n      try {\n        return JSON.parse(text);\n      } catch {\n        return { code: -1, message: \"Non-JSON response (HTTP \" + res.status + \"): \" + text.slice(0, 200) };\n      }\n    }\n  `);\n}\nexport async function getSelfUid(page) {\n    const nav = await getNavData(page);\n    const mid = nav?.data?.mid;\n    if (!mid)\n        throw new AuthRequiredError('bilibili.com');\n    return String(mid);\n}\nexport async function resolveUid(page, input) {\n    if (/^\\d+$/.test(input))\n        return input;\n    // Search for user by name\n    const payload = await apiGet(page, '/x/web-interface/wbi/search/type', {\n        params: { search_type: 'bili_user', keyword: input },\n        signed: true,\n    });\n    if (!payload || typeof payload !== 'object' || Array.isArray(payload) || !payload.data || typeof payload.data !== 'object' || Array.isArray(payload.data) || !Object.hasOwn(payload.data, 'result')) {\n        throw new CommandExecutionError(`Bilibili user search returned malformed result for ${input}`);\n    }\n    const results = payload.data.result;\n    if (!Array.isArray(results)) {\n        throw new CommandExecutionError(`Bilibili user search returned malformed result for ${input}`);\n    }\n    if (results.length > 0) {","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/bilibili/utils.js#L258-L294","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Log in to bilibili.com and export fresh cookies (SESSDATA at minimum) into your cookie file/env, then retry.","Catch AuthRequiredError and run/instruct the login flow before calling commands that need identity.","Confirm cookies were added to the page's browser context for domain .bilibili.com.","If cookies look valid, hit https://api.bilibili.com/x/web-interface/nav manually and check data.isLogin/mid to debug."],"exampleFix":"// before\nconst uid = await getSelfUid(page); // throws if not logged in\n// after\nlet uid;\ntry {\n  uid = await getSelfUid(page);\n} catch (e) {\n  if (e instanceof AuthRequiredError) {\n    await loginFlow(page); // set fresh SESSDATA cookies\n    uid = await getSelfUid(page);\n  } else throw e;\n}","handlingStrategy":"try-catch","validationCode":"// preflight auth check\nconst nav = await (await fetch('https://api.bilibili.com/x/web-interface/nav', { headers: { cookie } })).json();\nif (!nav?.data?.mid) throw new Error('Not logged in: set SESSDATA cookie before calling uid/self');","typeGuard":"function isLoggedInNav(nav){ return !!nav?.data?.mid; }","tryCatchPattern":"try { const uid = await getSelfUid(page); } catch (e) { if (e instanceof AuthRequiredError) { await loginFlow(page); const uid = await getSelfUid(page); } else throw e; }","preventionTips":["Export fresh SESSDATA cookies after logging in and refresh them periodically.","Ensure cookies are injected into the browser context for .bilibili.com.","Avoid logging out in the source browser — it invalidates exported SESSDATA.","Gate identity-dependent commands behind a nav/isLogin preflight."],"tags":["auth","bilibili","cookies","login","session-expired"],"backgroundTag":"auth-required-error","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}