jackwener/OpenCLI · error · AuthRequiredError

Pixiv PHPSESSID cookie missing

Error message

Pixiv PHPSESSID cookie missing

What it means

verifyPixivIdentity first checks the browser context for a PHPSESSID cookie whose value has the logged-in form `<numeric userId>_...` (anonymous sessions are a bare hash). If no such cookie exists it throws AuthRequiredError, signalling the pixiv.net CLI needs an interactive login before it can verify identity.

Source

Thrown at clis/pixiv/auth.js:14

import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { registerSiteAuthCommands } from '../_shared/site-auth.js';

async function hasPixivSessionCookie(page) {
  const cookies = await page.getCookies({ url: 'https://www.pixiv.net' });
  // Anonymous PHPSESSID is a bare hash; logged-in form is `<userId>_<hash>`.
  // Require the numeric uid prefix so the login poll doesn't navigate away
  // from accounts.pixiv.net while the user is still signing in.
  return cookies.some(c => c.name === 'PHPSESSID' && /^\d+_/.test(c.value || ''));
}

async function verifyPixivIdentity(page) {
  if (!await hasPixivSessionCookie(page)) {
    throw new AuthRequiredError('pixiv.net', 'Pixiv PHPSESSID cookie missing');
  }
  await page.goto('https://www.pixiv.net/');
  await page.wait(2);
  const probe = await page.evaluate(`(async () => {
    try {
      const meta = document.querySelector('meta[name="global-data"]')?.getAttribute('content') || '';
      let userData = null;
      if (meta) { try { userData = JSON.parse(meta); } catch {} }
      const u = userData?.userData;
      if (u?.id) {
        return { ok: true, user_id: String(u.id), name: String(u.name || u.account || '') };
      }
      const r = await fetch('/ajax/user/extra', { credentials: 'include', headers: { Accept: 'application/json' } });
      if (r.status === 401 || r.status === 403) {
        return { kind: 'auth', detail: 'Pixiv /ajax/user/extra HTTP ' + r.status };
      }
      if (!r.ok) return { kind: 'http', httpStatus: r.status };
      const d = await r.json();

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Run the pixiv login flow (`opencli pixiv login`) and complete sign-in at accounts.pixiv.net.
  2. Verify a `\d+_...`-shaped PHPSESSID cookie exists for https://www.pixiv.net in the browser profile.
  3. Persist/reuse the same browser profile directory between runs so the session cookie survives.
  4. Re-login if the session expired — pixiv rotates PHPSESSID periodically.
Defensive patterns

Strategy: validation

Validate before calling

const cookies = await page.getCookies({ url: 'https://www.pixiv.net' });
const loggedIn = cookies.some(c => c.name === 'PHPSESSID' && /^\d+_/.test(c.value || ''));
if (!loggedIn) await runLoginFlow(); // e.g. opencli pixiv login

Type guard

function hasPixivSessionCookie(cookies) {
  return cookies.some(c => c.name === 'PHPSESSID' && /^\d+_/.test(c.value || ''));
}

Try / catch

try {
  await pixivWhoAmI();
} catch (err) {
  if (String(err.message).includes('PHPSESSID cookie missing')) {
    await interactiveLogin('https://accounts.pixiv.net/login');
    await pixivWhoAmI();
  } else throw err;
}

Prevention

When it happens

Trigger: Running any pixiv auth-required command when the browser profile has no pixiv.net PHPSESSID cookie, or only an anonymous (non `\d+_`) PHPSESSID — never logged in, cookies cleared, or login still in progress.

Common situations: Fresh machine/CI container without a persisted browser profile; user cleared cookies; headless run before completing the accounts.pixiv.net login flow; expired session where pixiv dropped the cookie entirely.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/7b6b170c734b6ec7. Report an issue: GitHub.