全面修复: 所有微博API调用统一跳过首页+不跟随重定向, 彻底解决SSO误判问题

This commit is contained in:
2026-04-17 09:19:45 +08:00
parent 7e1c389a11
commit 36bb86a8f7
2 changed files with 33 additions and 20 deletions

View File

@@ -411,7 +411,7 @@ async def _async_do_signin(account_id: str, cron_expr: str = ""):
signed = already = failed = 0
log_entries = []
async with httpx.AsyncClient(timeout=15, follow_redirects=True) as client:
async with httpx.AsyncClient(timeout=15, follow_redirects=False) as client:
# 直接从 Cookie 获取 XSRF token不访问首页
xsrf = cookies.get("XSRF-TOKEN", "")
@@ -592,6 +592,11 @@ async def _do_single_signin(client, cookies: dict, topic: dict, xsrf: str) -> di
},
headers=h, cookies=cookies,
)
# 被重定向 = Cookie 失效
if resp.status_code in (301, 302):
return {"status": "failed", "message": "签到API被重定向Cookie可能失效"}
try:
data = resp.json()
except Exception:
@@ -746,21 +751,23 @@ async def _build_daily_report() -> str:
remain_days = (expire_dt - now).days
expire_str = expire_dt.strftime("%m-%d")
# 真实 API 验证
# 真实 API 验证(不跟随重定向,直接请求 AJAX API
real_valid = False
try:
async with _httpx.AsyncClient(timeout=10, follow_redirects=True) as hc:
async with _httpx.AsyncClient(timeout=10, follow_redirects=False) as hc:
vresp = await hc.get(
"https://weibo.com/ajax/side/cards",
params={"count": "1"},
headers=WEIBO_HEADERS,
headers={**WEIBO_HEADERS, "X-Requested-With": "XMLHttpRequest"},
cookies=cookie_dict,
)
try:
vdata = vresp.json()
real_valid = vdata.get("ok") == 1
except Exception:
real_valid = False
if vresp.status_code == 200:
try:
vdata = vresp.json()
real_valid = vdata.get("ok") == 1
except Exception:
real_valid = False
# 302 = Cookie 失效
except Exception:
real_valid = False