关键修复: 跳过weibo.com首页直接请求API, 避免SSO重定向导致Cookie误判失效
This commit is contained in:
@@ -73,25 +73,14 @@ async def main():
|
||||
|
||||
await engine.dispose()
|
||||
|
||||
# 测试 1: 访问 weibo.com
|
||||
print("\n--- 测试 1: GET https://weibo.com/ ---")
|
||||
async with httpx.AsyncClient(timeout=20, follow_redirects=True) as client:
|
||||
resp = await client.get("https://weibo.com/", headers=WEIBO_HEADERS, cookies=cookies)
|
||||
final_url = str(resp.url)
|
||||
print(f" 状态码: {resp.status_code}")
|
||||
print(f" 最终URL: {final_url}")
|
||||
print(f" 是否登录页: {'login.sina.com.cn' in final_url or 'passport' in final_url}")
|
||||
|
||||
if "login.sina.com.cn" in final_url or "passport" in final_url:
|
||||
print("\n❌ Cookie 已失效,被重定向到登录页")
|
||||
return
|
||||
|
||||
xsrf = client.cookies.get("XSRF-TOKEN", "")
|
||||
print(f" XSRF-TOKEN: {'有' if xsrf else '无'}")
|
||||
|
||||
# 测试 2: 获取超话列表
|
||||
print("\n--- 测试 2: 获取超话列表 ---")
|
||||
headers = {**WEIBO_HEADERS, "X-Requested-With": "XMLHttpRequest"}
|
||||
# 测试 1: 直接请求超话 API(不经过首页)
|
||||
print("\n--- 测试 1: 直接请求超话 API ---")
|
||||
async with httpx.AsyncClient(timeout=20, follow_redirects=False) as client:
|
||||
xsrf = cookies.get("XSRF-TOKEN", "")
|
||||
headers = {
|
||||
**WEIBO_HEADERS,
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
}
|
||||
if xsrf:
|
||||
headers["X-XSRF-TOKEN"] = xsrf
|
||||
|
||||
@@ -101,7 +90,19 @@ async def main():
|
||||
headers=headers, cookies=cookies,
|
||||
)
|
||||
print(f" 状态码: {resp.status_code}")
|
||||
print(f" 最终URL: {resp.url}")
|
||||
print(f" URL: {resp.url}")
|
||||
|
||||
if resp.status_code in (301, 302):
|
||||
print(f" 重定向到: {resp.headers.get('location', '')[:120]}")
|
||||
print("\n❌ Cookie 已失效(API 被重定向)")
|
||||
|
||||
# 对比:访问首页看看
|
||||
print("\n--- 对比: GET https://weibo.com/ ---")
|
||||
async with httpx.AsyncClient(timeout=20, follow_redirects=True) as c2:
|
||||
r2 = await c2.get("https://weibo.com/", headers=WEIBO_HEADERS, cookies=cookies)
|
||||
print(f" 最终URL: {r2.url}")
|
||||
print(f" 是否登录页: {'login.sina' in str(r2.url) or 'passport' in str(r2.url)}")
|
||||
return
|
||||
|
||||
try:
|
||||
data = resp.json()
|
||||
@@ -112,9 +113,11 @@ async def main():
|
||||
title = t.get("topic_name", "") or t.get("title", "")
|
||||
print(f" - {title}")
|
||||
if topics:
|
||||
print("\n✅ Cookie 有效,超话获取正常")
|
||||
print("\n✅ Cookie 有效,超话获取正常(直接 API 模式)")
|
||||
elif data.get("ok") == 1:
|
||||
print("\n⚠️ Cookie 有效但没有关注超话")
|
||||
else:
|
||||
print("\n⚠️ Cookie 可能有效但没有关注超话")
|
||||
print(f"\n❌ API 返回异常: {str(data)[:200]}")
|
||||
except Exception as e:
|
||||
print(f" ❌ 响应非 JSON: {resp.text[:300]}")
|
||||
print(f"\n❌ 获取超话失败: {e}")
|
||||
|
||||
Reference in New Issue
Block a user