{"record":{"id":"7a278a84571204ad","repo":"jackwener/OpenCLI","slug":"browser-session-required-for-bilibili-following","errorCode":null,"errorMessage":"Browser session required for bilibili following","messagePattern":"Browser session required for bilibili following","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/bilibili/following.js","lineNumber":19,"sourceCode":"import { cli, Strategy } from '@jackwener/opencli/registry';\nimport { CommandExecutionError } from '@jackwener/opencli/errors';\nimport { fetchJson, getSelfUid, resolveUid } from './utils.js';\ncli({\n    site: 'bilibili',\n    name: 'following',\n    access: 'read',\n    description: '获取 Bilibili 用户的关注列表',\n    domain: 'www.bilibili.com',\n    strategy: Strategy.COOKIE,\n    args: [\n        { name: 'uid', positional: true, required: false, help: '目标用户 ID（默认为当前登录用户）' },\n        { name: 'page', type: 'int', required: false, default: 1, help: '页码' },\n        { name: 'limit', type: 'int', required: false, default: 50, help: '每页数量 (最大 50)' },\n    ],\n    columns: ['mid', 'name', 'sign', 'following', 'fans'],\n    func: async (page, kwargs) => {\n        if (!page)\n            throw new CommandExecutionError('Browser session required for bilibili following');\n        // 1. Resolve UID (default to self)\n        const uid = kwargs.uid\n            ? await resolveUid(page, kwargs.uid)\n            : await getSelfUid(page);\n        const pn = kwargs.page ?? 1;\n        const ps = Math.min(kwargs.limit ?? 50, 50);\n        // 2. Fetch following list (standard Cookie API, no Wbi signing needed)\n        const payload = await fetchJson(page, `https://api.bilibili.com/x/relation/followings?vmid=${uid}&pn=${pn}&ps=${ps}&order=desc`);\n        if (payload.code !== 0) {\n            throw new CommandExecutionError(`获取关注列表失败: ${payload.message} (${payload.code})`);\n        }\n        const list = payload.data?.list || [];\n        if (list.length === 0) {\n            return [{ mid: '-', name: `共 ${payload.data?.total ?? 0} 人关注，当前页无数据`, sign: '', following: '', fans: '' }];\n        }\n        // 3. Map to output\n        return list.map((u) => ({\n            mid: u.mid,","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/bilibili/following.js#L1-L37","documentation":"Like follow, the following command needs an authenticated browser page to call getSelfUid (or resolveUid) and read the Cookie-authenticated followings API. A falsy page aborts immediately with this CommandExecutionError.","triggerScenarios":"Invoking bilibili following without an active/logged-in browser session, or when browser launch failed silently.","commonSituations":"CI pipelines without a browser profile, expired session where the page handle was discarded, calling the func programmatically with page=null.","solutions":["Establish the browser session (login) before running the command.","Pass a UID explicitly if supported, but the page requirement still stands — restart the session.","Check that your automation keeps the page/browser alive between calls."],"exampleFix":"// before\nconst rows = await cli.func(null, { uid: '9469745' });\n// after\nconst page = await getSession().ensurePage();\nconst rows = await cli.func(page, { uid: '9469745' });","handlingStrategy":"validation","validationCode":"if (!session?.page) throw new Error('establish a logged-in browser session before listing followings');","typeGuard":"function hasLivePage(ctx) {\n  return ctx != null && ctx.page != null && !ctx.page.isClosed?.();\n}","tryCatchPattern":"try {\n  await followingCommand(page, kwargs);\n} catch (e) {\n  if (String(e.message).includes('Browser session required')) {\n    const p = await ensureLoggedInPage();\n    return followingCommand(p, kwargs);\n  }\n  throw e;\n}","preventionTips":["Login before querying relation data (Cookie-authenticated API)","Verify the page handle is not closed before each command","Persist a logged-in browser profile for automation runs"],"tags":["session","authentication","browser"],"backgroundTag":"missing-browser-session","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}