{"record":{"id":"75ff39ba4dbf399e","repo":"jackwener/OpenCLI","slug":"auth-required-75ff39","errorCode":"AUTH_REQUIRED","errorMessage":"AUTH_REQUIRED: ${label} API failed: ${message}","messagePattern":"AUTH_REQUIRED: (.+?) API failed: (.+?)","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clis/tiktok/utils.js","lineNumber":254,"sourceCode":"  const text = await res.text();\n  if (!res.ok) {\n    throw new Error('HTTP ' + res.status + ' from ' + requestUrl + ': ' + text.slice(0, 160));\n  }\n  if (!text.trim()) return {};\n  try {\n    return JSON.parse(text);\n  } catch (error) {\n    throw new Error('invalid JSON from ' + requestUrl + ': ' + (error instanceof Error ? error.message : String(error)));\n  }\n}\n\nfunction assertTikTokApiSuccess(data, label) {\n  if (!data || typeof data !== 'object') return;\n  const code = data.status_code ?? data.statusCode;\n  if (code === undefined || code === null || Number(code) === 0) return;\n  const message = cleanText(data.status_msg ?? data.statusMsg ?? data.message ?? data.msg ?? code, 240);\n  if (Number(code) === 8 || /auth|captcha|login|permission|unauthori[sz]ed|forbidden/i.test(message)) {\n    throw new Error('AUTH_REQUIRED: ' + label + ' API failed: ' + message);\n  }\n  throw new Error(label + ' API failed: ' + message);\n}\n\nfunction findUniversalData() {\n  const scripts = Array.from(document.querySelectorAll('script'));\n  for (const script of scripts) {\n    const text = script.textContent || '';\n    if (!text || text.length < 32) continue;\n    const trimmed = text.trim();\n    if (!(trimmed.startsWith('{') || trimmed.startsWith('['))) continue;\n    if (\n      !text.includes('webapp.user-detail') &&\n      !text.includes('webapp.recommend-feed') &&\n      !text.includes('webapp.live-discover') &&\n      !text.includes('ItemModule') &&\n      !text.includes('itemList') &&\n      !text.includes('userInfo') &&","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/tiktok/utils.js#L236-L272","documentation":"assertTikTokApiSuccess inspects status_code/statusCode in a TikTok API payload; when the code is 8 or the status message matches auth/captcha/login/permission patterns it throws an 'AUTH_REQUIRED: <label> API failed: <msg>' error. This signals the API call was rejected because the session is not authenticated or is challenged.","triggerScenarios":"A TikTok API response arrived with a non-zero status_code equal to 8, or status_msg/statusMsg/message/msg matched /auth|captcha|login|permission|unauthorized|forbidden/i.","commonSituations":"Cookies expired mid-scrape so the API returns an auth error code; TikTok served a captcha challenge; the account lacks permission for the requested data; API schema change renamed status fields so a real success is misread — though that yields the generic branch instead.","solutions":["Re-authenticate the browser session / refresh cookies and rerun the command","Open tiktok.com in the automated browser and complete any captcha manually","Check status_msg after the prefix for the exact permission being denied","Confirm the response really contains status_code (schema change could feed wrong values)"],"exampleFix":"// before\nconst data = await fetchJson(url);\nassertTikTokApiSuccess(data, 'user/detail');\n// after\ntry {\n  const data = await fetchJson(url);\n  assertTikTokApiSuccess(data, 'user/detail');\n} catch (e) {\n  if (e.message.startsWith('AUTH_REQUIRED')) await refreshSession();\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"const loggedIn = await page.evaluate(() => document.cookie.includes('sessionid'));\nif (!loggedIn) throw new Error('AUTH_REQUIRED: log in to tiktok.com first');","typeGuard":"function isAuthRequiredPayload(data) {\n  const code = Number(data?.status_code ?? data?.statusCode ?? 0);\n  const msg = String(data?.status_msg ?? data?.statusMsg ?? data?.message ?? data?.msg ?? '');\n  return code === 8 || /auth|captcha|login|permission|unauthori[sz]ed|forbidden/i.test(msg);\n}","tryCatchPattern":"try { assertTikTokApiSuccess(data, label); } catch (e) {\n  if (e.message.startsWith('AUTH_REQUIRED')) { await refreshSession(); return retry(label); }\n  throw e;\n}","preventionTips":["Refresh session cookies before long scrape runs","Complete captchas manually when they appear","Throttle request rate to avoid challenges","Validate response schema so status fields are read correctly"],"tags":["auth","tiktok","api","session"],"backgroundTag":"auth-required-session-expired","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}