From 2ed56bb1443e37fd825bc89c85f0e5c453bdcd0a Mon Sep 17 00:00:00 2001 From: Qiufeng Date: Fri, 21 Aug 2026 17:54:29 +0800 Subject: [PATCH] =?UTF-8?q?fix(tencent):=20=E7=99=BB=E5=BD=95=E6=80=81?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E7=BB=9F=E4=B8=80=E6=94=B9=E4=B8=BA=20get=5F?= =?UTF-8?q?auth=5Finfo=20=E6=8E=A5=E5=8F=A3=E6=8E=A2=E6=B5=8B=EF=BC=88errC?= =?UTF-8?q?ode=3D=3D0=EF=BC=89=EF=BC=8C=E6=B6=88=E9=99=A4=E6=B5=8B?= =?UTF-8?q?=E8=AF=95/=E5=8F=91=E5=B8=83=E4=B8=A4=E5=A5=97=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E4=B8=8D=E4=B8=80=E8=87=B4=EF=BC=9B=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=80=81=20cookie=20=E4=B8=BA=20sessionid+wx?= =?UTF-8?q?uin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- myUtils/auth.py | 19 +++-------- uploader/tencent_uploader/main.py | 56 +++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/myUtils/auth.py b/myUtils/auth.py index 03a364c..b37a392 100644 --- a/myUtils/auth.py +++ b/myUtils/auth.py @@ -41,21 +41,10 @@ async def cookie_auth_douyin(account_file): async def cookie_auth_tencent(account_file): - async with async_playwright() as playwright: - browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS) - context = await browser.new_context(storage_state=account_file) - context = await set_init_script(context) - # 创建一个新的页面 - page = await context.new_page() - # 访问指定的 URL - await page.goto("https://channels.weixin.qq.com/platform/post/create") - try: - await page.wait_for_selector('div.title-name:has-text("微信小店")', timeout=5000) # 等待5秒 - tencent_logger.error("[+] 等待5秒 cookie 失效") - return False - except: - tencent_logger.success("[+] cookie 有效") - return True + # 统一复用 uploader 的 cookie_auth(接口探测优先 + 页面兜底), + # 确保「账号测试」与「发布」使用同一套登录态判定口径 + from uploader.tencent_uploader.main import cookie_auth as _tc_cookie_auth + return await _tc_cookie_auth(str(account_file)) async def cookie_auth_ks(account_file): diff --git a/uploader/tencent_uploader/main.py b/uploader/tencent_uploader/main.py index 82ff074..7ff7bfd 100644 --- a/uploader/tencent_uploader/main.py +++ b/uploader/tencent_uploader/main.py @@ -111,8 +111,60 @@ def format_str_for_short_title(origin_title: str) -> str: return formatted_string +# 视频号助手登录态探测接口:errCode==0 表示登录有效,非 0(如 300330)表示失效/未登录 +_TENCENT_AUTH_INFO_URL = "https://channels.weixin.qq.com/cgi-bin/mmfinderassistant-bin/auth/get_auth_info" +_TENCENT_AUTH_HEADERS = { + "Content-Type": "application/json", + "Origin": "https://channels.weixin.qq.com", + "Referer": "https://channels.weixin.qq.com/platform", + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36", +} + + +def _cookie_auth_via_api(account_file) -> bool | None: + """纯 HTTP 接口探测登录态(不开浏览器,快且稳)。 + + 返回 True=有效、False=失效、None=探测失败(网络/响应异常,需降级页面行为)。 + """ + try: + import json + import requests + + with open(account_file, "r", encoding="utf-8") as fobj: + state = json.load(fobj) + cookies = {c["name"]: c["value"] for c in state.get("cookies", [])} + resp = requests.post( + _TENCENT_AUTH_INFO_URL, + headers=_TENCENT_AUTH_HEADERS, + cookies=cookies, + json={}, + timeout=10, + ) + body = resp.json() + if body.get("errCode") == 0: + return True + if "errCode" in body: + return False + return None + except Exception as exc: + tencent_logger.warning(_msg("⚠️", f"接口探测登录态异常({exc}),降级为页面行为检测")) + return None + + async def cookie_auth(account_file): + """视频号登录态校验:接口探测优先(统一「测试」与「发布」口径),失败再降级页面行为。""" account_file = _resolve_account_file(account_file) + + # 1) 纯 HTTP 接口探测(快、准、统一,不开浏览器) + via_api = _cookie_auth_via_api(account_file) + if via_api is True: + tencent_logger.success(_msg("🥳", "cookie 有效(接口探测 errCode=0)")) + return True + if via_api is False: + tencent_logger.info(_msg("🥹", "cookie 已失效(接口探测 errCode≠0),得重新登录一下")) + return False + + # 2) 接口探测失败,降级为页面行为检测 async with async_playwright() as playwright: browser = await playwright.chromium.launch(**_build_launch_kwargs(headless=True)) try: @@ -235,9 +287,9 @@ async def _save_tencent_qrcode(page: Page, account_file: str, previous_qrcode_pa return qrcode_info -# 微信扫码登录成功后,视频号助手(channels.weixin.qq.com)域下必然种下这些登录态 cookie; +# 微信扫码登录成功后,视频号助手(channels.weixin.qq.com)域下必然种下 sessionid + wxuin; # 用 cookie 判定登录成功最可靠,不依赖页面 DOM/URL(登录后落地页可能停在首页、残留 iframe 等) -_TENCENT_LOGIN_COOKIE_MARKERS = {"ww_datasalt", "ww_bizuin", "wxuin", "slave_sid", "slave_user"} +_TENCENT_LOGIN_COOKIE_MARKERS = {"sessionid", "wxuin"} async def _has_tencent_login_cookie(context) -> bool: