fix(tencent): 登录态校验统一改为 get_auth_info 接口探测(errCode==0),消除测试/发布两套判定不一致;修正登录态 cookie 为 sessionid+wxuin

This commit is contained in:
Qiufeng
2026-08-21 17:54:29 +08:00
parent 4d9bf31260
commit 2ed56bb144
2 changed files with 58 additions and 17 deletions
+4 -15
View File
@@ -41,21 +41,10 @@ async def cookie_auth_douyin(account_file):
async def cookie_auth_tencent(account_file): async def cookie_auth_tencent(account_file):
async with async_playwright() as playwright: # 统一复用 uploader 的 cookie_auth(接口探测优先 + 页面兜底),
browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS) # 确保「账号测试」与「发布」使用同一套登录态判定口径
context = await browser.new_context(storage_state=account_file) from uploader.tencent_uploader.main import cookie_auth as _tc_cookie_auth
context = await set_init_script(context) return await _tc_cookie_auth(str(account_file))
# 创建一个新的页面
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
async def cookie_auth_ks(account_file): async def cookie_auth_ks(account_file):
+54 -2
View File
@@ -111,8 +111,60 @@ def format_str_for_short_title(origin_title: str) -> str:
return formatted_string 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): async def cookie_auth(account_file):
"""视频号登录态校验:接口探测优先(统一「测试」与「发布」口径),失败再降级页面行为。"""
account_file = _resolve_account_file(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: async with async_playwright() as playwright:
browser = await playwright.chromium.launch(**_build_launch_kwargs(headless=True)) browser = await playwright.chromium.launch(**_build_launch_kwargs(headless=True))
try: try:
@@ -235,9 +287,9 @@ async def _save_tencent_qrcode(page: Page, account_file: str, previous_qrcode_pa
return qrcode_info return qrcode_info
# 微信扫码登录成功后,视频号助手(channels.weixin.qq.com)域下必然种下这些登录态 cookie; # 微信扫码登录成功后,视频号助手(channels.weixin.qq.com)域下必然种下 sessionid + wxuin;
# 用 cookie 判定登录成功最可靠,不依赖页面 DOM/URL(登录后落地页可能停在首页、残留 iframe 等) # 用 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: async def _has_tencent_login_cookie(context) -> bool: