jackwener / jackwener/xiaohongshu-cli

建议:让 COOKIE_TTL_DAYS 可配置/可关闭,并支持从外部直接注入 Cookie(避免每 7 天被迫重新登录)

Open
#82 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.6k
Forks
272
PR merge metrics
No merged PRs in 30d

Description

## 问题
在 `xhs_cli/cookies.py` 中,`COOKIE_TTL_DAYS = 7` 是一个硬性的"重新校验"触发点:

- 保存的 cookie 超过 7 天后,每次调用都会在过期逻辑里尝试从本地浏览器偷读小红书 Cookie 来"续期";
- 续期依赖 `browser-cookie3`,虽然本工具支持 Chrome / Arc / Edge / Firefox / Safari / Brave / Chromium / Opera / Vivaldi 等(比 bilibili-cli 多),但**仍覆盖不了隔离浏览器 / Agent 浏览器(如 ego-browser)或任何非 browser-cookie3 支持的来源**;
- 如果用户的登录态只在这些不被支持的来源里,自动续期失败 → 每 7 天被迫重新扫码 / 重新登录。

## 这其实是"人造"限制
小红书的 `a1` / `web_session` 等 Cookie **本身并不会 7 天失效**(与 B 站同理,参考 bilibili-API-collect 的 `docs/login/cookie_refresh.md`:cookie 不会主动刷新,只有在真的被风控强制下线前才变化)。7 天只是 CLI 自己设的校验间隔,对"登录态不在被支持浏览器里"的用户反而造成反复重新登录的糟糕体验。

## 复现
1. 在没有被 browser-cookie3 支持的浏览器里登录小红书(例如隔离/Agent 浏览器),用 `xhs login`(或手动写 `~/.xiaohongshu-cli/cookies.json`)注入 cookie;
2. 7 天后任意调用 `xhs status` / `xhs search`;
3. 触发过期逻辑,自动续期失败,随后可能被要求重新登录。

## 建议的改进
1. **把 TTL 做成可配置**:支持环境变量(如 `XHS_COOKIE_TTL_DAYS`)或配置项,`0` 表示永不强制过期(仅在小红书真正返回未登录时再清 cookie)。
2. **支持从外部直接注入 Cookie**:允许通过环境变量(如 `XHS_COOKIE`,参考 tamnd/xiaohongshu-cli 的做法)或 `--cookie` 参数直接注入一份长期有效的 cookie,绕过 QR。这样即使用户从任意已登录浏览器(含隔离/Agent 浏览器)抠出 cookie,也能一次性让 CLI 长期可用。
3. (可选)给续期逻辑在 `browser-cookie3` 之外,增加 CDP 读取(见下),顺带支持任意带 CDP 的浏览器。

## 我们目前的临时 workaround(已验证可用)
- 把 `COOKIE_TTL_DAYS` 改成极大值(如 `36500`);
- 用 CDP 从已登录的浏览器会话里读出全部小红书 Cookie(含 HttpOnly 的 `web_session`、`a1` 等),直接写入 `~/.xiaohongshu-cli/cookies.json`(字段即 cookie 名→值,加 `saved_at`)。

验证:`xhs status` → `authenticated: true`,账号正常返回;`xhs search "美食"` → 正常返回笔记列表。登录自此长期保持,直到账号被风控强制下线。

希望能把 TTL 改成可配置 + 支持外部注入,这对"登录态在不被 browser-cookie3 支持的浏览器里"的用户会友好很多。谢谢!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in xhs_cli/cookies.py and trace the expiry path used by xhs status and xhs search, along with the existing xhs login flow. Define how a configurable or disabled TTL and externally supplied cookies should interact with stored cookies and genuine unauthenticated responses. Done means supported configuration and injection paths work without forced browser renewal, while existing login behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
authentication, cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.