Cookie Extraction from Chrome/Firefox for Platform Authentication in Agent-Reach

Agent-Reach extracts authentication cookies directly from Chrome, Firefox, Edge, Brave, and Opera to configure Twitter/X, XiaoHongShu, Bilibili, and Xueqiu platforms without manual copying.

Agent-Reach provides a built-in utility that automates cookie extraction from Chrome/Firefox for platform authentication, eliminating the need to manually copy cookie strings from browser developer tools. The implementation resides in agent_reach/cookie_extract.py and supports multiple browser backends including rookiepy and browser_cookie3.

The core extraction logic in agent_reach/cookie_extract.py follows a three-phase pipeline: browser selection, backend retrieval, and platform-specific filtering.

Browser Selection and Validation

The system accepts browser names (chrome, firefox, edge, brave, opera) through the CLI command agent-reach configure --from-browser <browser> or the Python function configure_from_browser(browser, config). Input validation normalizes the browser name to lower-case before processing.

Dual Backend Architecture

Agent-Reach implements a failover mechanism for cookie retrieval:

  1. rookiepy (Rust-based extractor) — Primary backend offering faster performance
  2. browser_cookie3 (Python-based) — Fallback option for broader compatibility

If neither library is present, the code raises a RuntimeError with explicit installation instructions:

RuntimeError: Cookie extraction requires rookiepy or browser-cookie3.
Install: pip install rookiepy  (recommended)
     or: pip install browser-cookie3

Platform Specifications and Filtering

The PLATFORM_SPECS dictionary (lines 15-41) defines extraction rules for each supported platform:

  • Twitter/X: Extracts auth_token and ct0 from specific domains
  • XiaoHongShu: Captures all cookies for the domain (returns as concatenated string)
  • Bilibili: Extracts SESSDATA and bili_jct tokens
  • Xueqiu: Captures full cookie set for the domain

The filtering logic iterates through all browser cookies, matching domains against spec["domains"] patterns. When spec["cookies"] is a list, only those specific names are retained; when None, the full cookie set is formatted as a header-style string (name=value; ...).

Practical Usage Examples

Command Line Extraction

Execute the built-in CLI command to extract cookies from Chrome and populate the configuration:

agent-reach configure --from-browser chrome

The command prints a summary for each platform and updates ~/.agent-reach/config.yaml automatically.

Programmatic Configuration

Integrate cookie extraction into Python applications using the configure_from_browser function:

from agent_reach.config import Config
from agent_reach.cookie_extract import configure_from_browser

cfg = Config()  # Loads existing config or creates new instance

results = configure_from_browser("firefox", cfg)

for platform, ok, msg in results:
    status = "✅" if ok else "❌"
    print(f"{status} {platform}: {msg}")

# Verify configuration status

if cfg.is_configured("twitter_xreach"):
    print("Twitter/X authentication ready")

For raw cookie payloads without configuration storage:

from agent_reach.cookie_extract import extract_all

cookies = extract_all("chrome")
twitter_token = cookies["twitter"]["auth_token"]
xhs_header = cookies["xhs"]["cookie_string"]  # Full header format

Configuration Integration and Legacy Sync

The configure_from_browser function (lines 25-90) handles the complete integration pipeline:

  1. Calls extract_all(browser) to retrieve raw cookies
  2. Populates a Config instance (from agent_reach/config.py) with extracted values
  3. Syncs Twitter credentials to legacy tools via _sync_xfetch_session and _sync_bird_env methods

The result structure maps each config_key to a sub-dictionary of extracted values:

{
    "twitter": {"auth_token": "...", "ct0": "..."},
    "xhs": {"cookie_string": "a=1; b=2; ..."},
    "bilibili": {"SESSDATA": "...", "bili_jct": "..."},
    "xueqiu": {"cookie_string": "..."}
}

Error Handling and Dependencies

The extraction system requires either rookiepy or browser-cookie3 installed in the Python environment. The code attempts import in order and provides clear error messaging when dependencies are missing. Browser profile permissions may affect extraction success on certain operating systems, handled by the test suite in tests/test_cookie_extract_perms.py.

Summary

  • Agent-Reach automates cookie extraction from Chrome, Firefox, Edge, Brave, and Opera using agent_reach/cookie_extract.py
  • Dual backend support prioritizes rookiepy with browser_cookie3 as fallback
  • Platform-specific filtering extracts exact tokens for Twitter/X and Bilibili, or full cookie strings for XiaoHongShu and Xueqiu
  • CLI command agent-reach configure --from-browser chrome updates ~/.agent-reach/config.yaml automatically
  • Python API provides both high-level configuration (configure_from_browser) and low-level extraction (extract_all)

Frequently Asked Questions

Agent-Reach supports Chrome, Firefox, Edge, Brave, and Opera. The system validates browser names case-insensitively and uses the appropriate extraction method for each browser's cookie storage format.

What happens if neither rookiepy nor browser_cookie3 is installed?

The extract_all function raises a RuntimeError with specific installation instructions. Users must install either pip install rookiepy (recommended for performance) or pip install browser-cookie3 (alternative fallback).

The PLATFORM_SPECS configuration defines platform-specific rules. Twitter/X and Bilibili extract specific named cookies (auth_token, ct0, SESSDATA, bili_jct), while XiaoHongShu and Xueqiu capture complete cookie strings for their domains. This ensures compatibility with each platform's authentication requirements.

Can I extract cookies without updating the configuration file?

Yes. Use the extract_all(browser) function directly from agent_reach/cookie_extract.py to retrieve cookies as a dictionary without persisting to ~/.agent-reach/config.yaml. This returns raw tokens suitable for custom authentication flows or debugging.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →