{"record":{"id":"931485096aa2d817","repo":"jackwener/OpenCLI","slug":"www-youtube-com-931485","errorCode":null,"errorMessage":"www.youtube.com","messagePattern":"www\\.youtube\\.com","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/youtube/history.js","lineNumber":51,"sourceCode":"\ncli({\n    site: 'youtube',\n    name: 'history',\n    access: 'read',\n    description: 'Get YouTube watch history',\n    domain: 'www.youtube.com',\n    strategy: Strategy.COOKIE,\n    args: [\n        { name: 'limit', type: 'int', default: DEFAULT_LIMIT, help: 'Max videos to return (default 30, max 200)' },\n    ],\n    columns: ['rank', 'title', 'channel', 'views', 'duration', 'url'],\n    func: async (page, kwargs) => {\n        const limit = normalizeLimit(kwargs.limit);\n        await prepareYoutubeApiPage(page);\n\n        const sapisid = await readYoutubeSapisid(page);\n        if (!sapisid) {\n            throw new AuthRequiredError('www.youtube.com', 'Not logged in (SAPISID cookie missing)');\n        }\n\n        const result = await page.evaluate(`\n      (async () => {\n        ${SAPISID_HASH_FN}\n\n        const limit = ${limit};\n        const maxPages = ${MAX_PAGES};\n        const requestTimeoutMs = ${REQUEST_TIMEOUT_SECONDS * 1000};\n        const cfg = window.ytcfg?.data_ || {};\n        const apiKey = cfg.INNERTUBE_API_KEY;\n        const context = cfg.INNERTUBE_CONTEXT;\n        if (!apiKey || !context) {\n          return { error: 'config', message: 'YouTube InnerTube config not found' };\n        }\n\n        const authHash = await getSapisidHash(${JSON.stringify(sapisid)}, 'https://www.youtube.com');\n        if (!authHash) return { error: 'auth', message: 'Not logged in (SAPISID cookie missing)' };","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/youtube/history.js#L33-L69","documentation":"AuthRequiredError thrown by `youtube history` when the SAPISID cookie is missing from www.youtube.com. Watch history is a private, authenticated surface; the library needs the SAPISID cookie to compute the SAPISIDHASH authorization header for InnerTube API calls. No network request is made — the check fails before evaluation.","triggerScenarios":"Running `youtube history` with a browser profile/page that is not logged into YouTube, cookies cleared or expired, using a fresh automation profile without a Google session, or third-party-cookie blocking preventing cookie reads.","commonSituations":"CI/headless environments with a clean profile; logged out of Google in the automation browser; expired session after password change or security sign-out; connecting the scraper to the wrong user-data-dir without YouTube login.","solutions":["Log into www.youtube.com in the browser page/profile the library controls.","Point the automation at a persistent user-data-dir that retains the Google session.","Verify the SAPISID cookie exists (DevTools → Application → Cookies → youtube.com).","Re-login if the session expired (password change, security event) and retry."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const cookies = await page.context().cookies('https://www.youtube.com');\nif (!cookies.some(c => c.name === 'SAPISID')) {\n  throw new Error('Log into youtube.com first: SAPISID cookie missing');\n}","typeGuard":null,"tryCatchPattern":"try {\n  const history = await yt.history({ limit: 30 });\n} catch (e) {\n  if (e instanceof AuthRequiredError) {\n    throw new Error('Open the automation browser and log into youtube.com, then retry');\n  }\n  throw e;\n}","preventionTips":["Log into youtube.com in the exact profile/page the library drives.","Use a persistent user-data-dir so sessions survive restarts.","Check the SAPISID cookie exists before batch runs.","Re-login after password changes or Google security sign-outs."],"tags":["auth","youtube","cookies","authentication-required"],"backgroundTag":"auth-required-login","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}