{"record":{"id":"2bfa898c1000b765","repo":"jackwener/OpenCLI","slug":"http-response-status-make-sure-you-are-logged","errorCode":null,"errorMessage":"HTTP ${response.status} - make sure you are logged in to Instagram","messagePattern":"HTTP (.+?) - make sure you are logged in to Instagram","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clis/instagram/_shared/user-id.js","lineNumber":26,"sourceCode":" */\nexport function buildResolveInstagramUserIdJs() {\n    return `\n  function normalizeInstagramUserId(value, label) {\n    const id = typeof value === 'number' ? String(value) : (typeof value === 'string' ? value.trim() : '');\n    if (!/^\\\\d+$/.test(id)) throw new Error(label);\n    return id;\n  }\n  async function readInstagramJson(response, label) {\n    try {\n      return await response.json();\n    } catch {\n      throw new Error(label + ' returned invalid JSON');\n    }\n  }\n  function throwInstagramHttpError(response, label, username) {\n    if (response.status === 404) throw new Error('User not found: ' + username);\n    if (response.status === 401 || response.status === 403) {\n      throw new Error('HTTP ' + response.status + ' - make sure you are logged in to Instagram');\n    }\n    throw new Error(label + ' failed: HTTP ' + response.status);\n  }\n  const r1 = await fetch('https://www.instagram.com/api/v1/users/web_profile_info/?username=' + encodeURIComponent(username), opts);\n  if (r1.status === 404) throw new Error('User not found: ' + username);\n  if (!r1.ok && r1.status !== 400) throwInstagramHttpError(r1, 'Instagram web_profile_info', username);\n  let userId = r1.ok ? normalizeInstagramUserId((await readInstagramJson(r1, 'Instagram web_profile_info'))?.data?.user?.id, 'Instagram web_profile_info returned no valid user id for: ' + username) : '';\n  if (!userId) {\n    const r1b = await fetch('https://www.instagram.com/api/v1/feed/user/' + encodeURIComponent(username) + '/username/?count=1', opts);\n    if (!r1b.ok) throwInstagramHttpError(r1b, 'Instagram feed-by-username', username);\n    userId = normalizeInstagramUserId((await readInstagramJson(r1b, 'Instagram feed-by-username'))?.user?.pk, 'Instagram feed returned no valid profile owner for: ' + username);\n  }`;\n}\n","sourceCodeStart":8,"sourceCodeEnd":40,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/_shared/user-id.js#L8-L40","documentation":"Thrown by throwInstagramHttpError when the Instagram web_profile_info (or feed-by-username) endpoint returns HTTP 401 or 403, meaning Instagram rejected the request as unauthenticated or forbidden. The library throws it because these statuses almost always mean the browser session is not logged in or the session cookie is no longer accepted by Instagram's API. It distinguishes this case from a plain HTTP failure (error 1931) so the user knows to authenticate first.","triggerScenarios":"fetch to https://www.instagram.com/api/v1/users/web_profile_info/?username=... (via throwInstagramHttpError on r1 or fallback r1b) returns 401/403 while resolving a username to a user id inside the page context, typically because the sessionid cookie is missing, expired, or flagged.","commonSituations":"Running an instagram CLI subcommand before running the site's `login` command; the saved Instagram session expired or was revoked (password change, 'logout of all sessions', Instagram server-side invalidation); Instagram rate-limiting or temporarily blocking the account's API access (403); using a headless profile whose cookies were cleared.","solutions":["Run the Instagram auth/login flow (clis/instagram auth login) and complete it in the browser so a valid sessionid cookie is stored","Verify cookies: check that a non-empty sessionid cookie exists for https://www.instagram.com (this is what hasInstagramSessionCookie checks)","Re-login if the session expired — change of password or Instagram revoking sessions invalidates the old cookie","If 403 persists after login, wait and retry later (Instagram may be rate-limiting/blocking the account) or use a different account","Retry the command after re-authenticating"],"exampleFix":"// before: running profile lookup without a session\n$ opencli instagram collection-list someuser\nError: HTTP 401 - make sure you are logged in to Instagram\n\n// after: authenticate first\n$ opencli instagram auth login\n$ opencli instagram collection-list someuser","handlingStrategy":"try-catch","validationCode":"// before calling the command, check the session cookie via the auth quick-check\nconst cookies = await page.getCookies({ url: 'https://www.instagram.com' });\nif (!cookies.some(c => c.name === 'sessionid' && c.value)) {\n  throw new Error('Not logged in to Instagram — run the login command first');\n}","typeGuard":"function hasInstagramSession(cookies) {\n  return Array.isArray(cookies) && cookies.some(\n    c => c && c.name === 'sessionid' && typeof c.value === 'string' && c.value.length > 0\n  );\n}","tryCatchPattern":"try {\n  await resolveInstagramUserId(username);\n} catch (e) {\n  if (/HTTP 40[13] - make sure you are logged in/.test(e.message)) {\n    await instagramLogin();          // re-authenticate\n    return resolveInstagramUserId(username);\n  }\n  throw e;\n}","preventionTips":["Always run the Instagram login/verify flow before commands that resolve user ids","Check for a non-empty sessionid cookie before issuing Instagram API calls","Re-login after a password change or Instagram session invalidation","Handle 401/403 centrally by triggering the login flow rather than failing the whole batch"],"tags":["http-401","http-403","authentication","instagram"],"backgroundTag":"instagram-session-not-logged-in","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}