{"record":{"id":"e23c5c1cbdbe0dd5","repo":"jackwener/OpenCLI","slug":"please-log-in-to-the-wechat-official-account-platf","errorCode":null,"errorMessage":"Please log in to the WeChat Official Account platform and retry.","messagePattern":"Please log in to the WeChat Official Account platform and retry\\.","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/weixin/create-draft.js","lineNumber":59,"sourceCode":"        throw new ArgumentError(`weixin create-draft cover-image does not exist: ${absPath}`);\n    }\n    if (!stat.isFile()) {\n        throw new ArgumentError(`weixin create-draft cover-image is not a file: ${absPath}`);\n    }\n    const extension = path.extname(absPath).toLowerCase();\n    const mimeType = IMAGE_MIME_TYPES.get(extension);\n    if (!mimeType) {\n        throw new ArgumentError('weixin create-draft cover-image must be JPEG, PNG, GIF, or WebP');\n    }\n    return { absPath, fileName: path.basename(absPath), mimeType };\n}\n\nasync function navigateToEditor(page) {\n    await page.goto(WEIXIN_HOME);\n    await page.wait(3);\n    const token = await evaluate(page, `(window.location.href.match(/token=(\\\\d+)/)||[])[1]`);\n    if (!token) {\n        throw new AuthRequiredError(WEIXIN_DOMAIN, 'Please log in to the WeChat Official Account platform and retry.');\n    }\n    await page.goto(`https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=77&token=${token}&lang=zh_CN`);\n    await page.wait(4);\n    const hasTitle = await evaluate(page, '!!document.querySelector(\"textarea#title\")');\n    if (hasTitle !== true) {\n        throw new CommandExecutionError('WeChat article editor did not load. The session may have expired.');\n    }\n}\n\nasync function fillField(page, selector, value) {\n    return evaluate(page, `(() => {\n        var el = document.querySelector(${JSON.stringify(selector)});\n        if (!el) return { ok: false, reason: 'field not found' };\n        el.focus();\n        var proto = el.tagName === 'TEXTAREA' ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype;\n        var setter = Object.getOwnPropertyDescriptor(proto, 'value');\n        if (setter && setter.set) setter.set.call(el, ${JSON.stringify(value)});\n        else el.value = ${JSON.stringify(value)};","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/weixin/create-draft.js#L41-L77","documentation":"navigateToEditor opens mp.weixin.qq.com and looks for a token= digits parameter in the URL, which only appears for an authenticated session. If no token is found it throws AuthRequiredError telling you to log in to the WeChat Official Account platform and retry.","triggerScenarios":"Running weixin create-draft while the automated browser has no valid mp.weixin.qq.com session; the login page/QR-scan page is shown instead of the dashboard so no token parameter exists.","commonSituations":"First run before ever logging in; WeChat Official Account sessions expire quickly (often daily); QR re-verification required after risk control; logged into a different account type without editor access.","solutions":["Open the automated browser, log in to mp.weixin.qq.com (QR scan), then rerun the command.","Reuse persisted browser profile/cookies so the session survives between runs.","Rerun shortly after re-login — sessions expire fast.","Verify the account has permission to create drafts (subscribe/account type restrictions)."],"exampleFix":"// before\nawait createDraft({ title, content }); // fails with AuthRequiredError\n// after\nawait loginWeixin(); // performs QR login first\nawait createDraft({ title, content });","handlingStrategy":"try-catch","validationCode":"const href = await page.evaluate('window.location.href');\nif (!/token=\\d+/.test(href)) throw new Error('Not authenticated on mp.weixin.qq.com — log in first');","typeGuard":"function hasWeixinToken(href) {\n  return typeof href === 'string' && /token=\\d+/.test(href);\n}","tryCatchPattern":"try {\n  await createDraft(input);\n} catch (err) {\n  if (err instanceof AuthRequiredError) {\n    console.error('Log in to the WeChat Official Account platform (QR scan), then retry');\n  } else throw err;\n}","preventionTips":["Persist the browser profile so WeChat sessions survive restarts","Re-login (QR scan) at the start of each automation session — sessions expire quickly","Check for token= in the URL before running draft creation","Ensure the account has draft-creation permissions"],"tags":["weixin","auth","login-required","session-expired"],"backgroundTag":"login-required","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}