jackwener/OpenCLI · error · AuthRequiredError

微信公众号草稿箱需要已登录的 mp.weixin.qq.com 会话

Error message

微信公众号草稿箱需要已登录的 mp.weixin.qq.com 会话

What it means

The `weixin drafts` command opens mp.weixin.qq.com and extracts the token=... parameter from the URL after page load. If no token is present in the final URL, the user does not have an active logged-in mp.weixin.qq.com session, so the command throws AuthRequiredError (code AUTH_REQUIRED, exit code 77) naming the WeChat mp domain. This guard exists because the drafts API endpoint only works with a session that already carries valid login cookies.

Source

Thrown at clis/weixin/drafts.js:26

    name: 'drafts',
    access: 'read',
    description: '列出微信公众号草稿箱',
    domain: WEIXIN_DOMAIN,
    strategy: Strategy.COOKIE,
    browser: true,
    navigateBefore: false,
    args: [
        { name: 'limit', type: 'int', default: 10, help: '最多显示条数' },
        { name: 'timeout', type: 'int', required: false, default: 60, help: 'Max seconds for the overall command (default: 60)' },
    ],
    columns: ['Index', 'Title', 'Time'],

    func: async (page, kwargs) => {
        await page.goto('https://mp.weixin.qq.com/');
        await page.wait(3);
        const token = await page.evaluate(`(window.location.href.match(/token=(\\d+)/)||[])[1]`);
        if (!token) {
            throw new AuthRequiredError(WEIXIN_DOMAIN, '微信公众号草稿箱需要已登录的 mp.weixin.qq.com 会话');
        }

        await page.goto(`https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=${kwargs.limit}&type=77&action=list_card&token=${token}&lang=zh_CN`);
        await page.wait(4);

        const drafts = await page.evaluate(`(() => {
            var results = [];
            var idx = 0;

            var cards = document.querySelectorAll('.weui-desktop-card');
            for (var i = 0; i < cards.length; i++) {
                if (cards[i].className.includes('card_new')) continue;
                var titleEl = cards[i].querySelector('[class*=title]');
                var timeEl = cards[i].querySelector('[class*=tips]');
                var title = titleEl ? titleEl.textContent.trim() : '';
                var time = timeEl ? timeEl.textContent.trim().replace(/\\s+/g, ' ') : '';
                if (title) results.push({ Index: ++idx, Title: title, Time: time });
            }

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Open Chrome/Chromium with the profile used by opencli and log in to mp.weixin.qq.com (scan QR code in the WeChat app)
  2. Re-run `opencli weixin drafts` after login so the token is present in the URL
  3. If you keep hitting this, avoid clearing cookies for that browser profile and re-login whenever WeChat expires the session

Example fix

// before
npx opencli weixin drafts  // fails: no mp session
// after
// 1) open browser, log in to https://mp.weixin.qq.com (scan QR)
// 2) retry
npx opencli weixin drafts
Defensive patterns

Strategy: try-catch

Validate before calling

// probe for a live session before calling drafts
try { await run(['weixin', 'drafts']); } catch (e) {
  if (e instanceof CliError && e.code === 'AUTH_REQUIRED') {
    console.log('Log in to https://mp.weixin.qq.com in the browser first');
  }
}

Type guard

function isAuthRequired(e) { return e instanceof CliError && e.code === 'AUTH_REQUIRED' && e.exitCode === 77; }

Try / catch

try {
  const drafts = await run(['weixin', 'drafts']);
} catch (e) {
  if (e instanceof CliError && e.code === 'AUTH_REQUIRED') {
    // open Chrome, log in to mp.weixin.qq.com (QR scan), then retry once
  } else throw e;
}

Prevention

When it happens

Trigger: Running `opencli weixin drafts` when the connected Chrome/Chromium profile has no logged-in mp.weixin.qq.com session: cookies expired, the account logged out, login scan on WeChat app was never done, or the page redirected to a login/QR-code page so window.location has no token= digits.

Common situations: WeChat Official Account sessions expire after a few days of inactivity; the browser profile was cleared of cookies; the automation connects a fresh profile that was never logged in; WeChat forces re-scan QR login after security policy updates.

Related errors


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