jackwener/OpenCLI · error · AuthRequiredError

taobao add-cart requires a logged-in Taobao session

Error message

taobao add-cart requires a logged-in Taobao session

What it means

In the taobao add-cart command, after clicking the add-to-cart button the page's text is scanned for success/spec/login markers. If it detects '请登录' (please log in), the command throws AuthRequiredError because adding to cart requires an authenticated Taobao session.

Source

Thrown at clis/taobao/add-cart.js:134

        for (let i = 0; i < 10; i++) {
          await new Promise(r => setTimeout(r, 500));
          const text = document.body?.innerText || '';
          if (text.includes('已加入购物车') || text.includes('商品已成功') || text.includes('去购物车结算') || text.includes('去购物车')) {
            return 'success';
          }
          if (text.includes('请选择') || text.includes('请先选择')) {
            return 'need_spec';
          }
          if (text.includes('请登录')) {
            return 'login_required';
          }
        }
        if (location.href.includes('cart')) return 'success';
        return 'unknown';
      })()
    `);
        if (result === 'login_required') {
            throw new AuthRequiredError('taobao add-cart requires a logged-in Taobao session');
        }
        let status = '? 未确认';
        if (result === 'success')
            status = '✓ 已加入购物车';
        else if (result === 'need_spec')
            status = '✗ 需要选择更多规格';
        const selectedSpec = Array.isArray(selectResult) ? selectResult.join(' | ') : '';
        return [{
                status,
                title: info?.title || '',
                price: info?.price || '',
                selected_spec: selectedSpec || '(未选择)',
                item_id: itemId,
            }];
    },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Run the Taobao login command (`taobao auth login`) and complete QR/password login in the browser.
  2. Verify the session with `taobao auth status` (tracknick cookie present) before retrying add-cart.
  3. Retry add-cart after login; if it still demands login, clear cookies and log in again.
  4. Check that the profile used by the CLI is the one you logged into.

Example fix

// before: attempting add-cart on an anonymous profile
await addCart(page, itemId);
// after: verify session first
import { verifyTaobaoIdentity } from '../taobao/auth.js';
try { await verifyTaobaoIdentity(page); await addCart(page, itemId); }
catch (e) { if (e instanceof AuthRequiredError) { await taobaoLogin(page); return addCart(page, itemId); } throw e; }
Defensive patterns

Strategy: validation

Validate before calling

// run before add-cart
import { verifyTaobaoIdentity } from '../taobao/auth.js';
await verifyTaobaoIdentity(page); // throws AuthRequiredError early if not logged in

Try / catch

try {
  await addCart(page, itemId);
} catch (e) {
  if (e instanceof AuthRequiredError) {
    await taobaoLogin(page);   // complete interactive login
    return addCart(page, itemId);
  }
  throw e;
}

Prevention

When it happens

Trigger: The in-page detector finds '请登录' in document.body.innerText within the 10x500ms polling window after clicking '加入购物车' — the browser profile has no valid Taobao login, so Taobao demands login at cart time.

Common situations: Fresh browser profile never logged into Taobao; expired Taobao session (cookies stale); Taobao forcing re-login on sensitive actions like cart adds; running the command before `taobao auth login`.

Related errors


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