How to Automatically Extract Cookies from Chrome or Firefox for Platform Authentication in Agent-Reach
Agent-Reach provides a built-in utility in agent_reach/cookie_extract.py that extracts authentication cookies from Chrome, Firefox, Edge, Brave, or Opera using rookiepy or browser_cookie3, then stores them in ~/.agent-reach/config.yaml for seamless platform integration.
The Agent-Reach repository simplifies social media automation by automatically extracting cookies from Chrome or Firefox for platform authentication. This eliminates manual cookie copying by pulling credentials directly from your local browser profiles and securely storing them for Twitter/X, XiaoHongShu, Bilibili, and Xueqiu integrations.
How Browser Cookie Extraction Works in Agent-Reach
The core implementation resides in agent_reach/cookie_extract.py, which orchestrates the entire extraction pipeline. The system validates your browser choice, attempts to retrieve cookies using Rust-based or Python-based backends, filters them according to platform-specific rules, and writes the results to your local configuration.
The PLATFORM_SPECS Configuration
Lines 15-41 define PLATFORM_SPECS, a static list that maps supported platforms to their required cookie patterns:
name: Human-readable platform labeldomains: Cookie domain patterns to match against browser datacookies: Specific cookie names to extract (e.g.,auth_token,ct0for Twitter), orNoneto grab all cookies for that domain (used for XiaoHongShu and Xueqiu)config_key: The key used when storing data in the Agent-Reach config
Backend Cookie Retrieval Libraries
The extraction logic implements a priority-based fallback strategy:
- Primary:
rookiepy(Rust-based extractor) — faster and more robust - Fallback:
browser_cookie3(Python-based) - Error: If neither is available, the code raises
RuntimeError: Cookie extraction requires rookiepy or browser-cookie3.
Using the CLI Command to Extract Cookies Automatically
The simplest method uses the CLI entry point defined in agent_reach/cli.py:
agent-reach configure --from-browser chrome
Supported browsers include chrome, firefox, edge, brave, and opera. This command iterates through the PLATFORM_SPECS, attempts to match domains and cookie names from your browser's SQLite stores, and writes the formatted results to ~/.agent-reach/config.yaml.
Programmatic Cookie Extraction from Python
For custom workflows, import the extraction functions directly from agent_reach/cookie_extract.
Configuring via configure_from_browser()
The configure_from_browser(browser, config) function (lines 25-90) handles the full pipeline:
from agent_reach.config import Config
from agent_reach.cookie_extract import configure_from_browser
cfg = Config() # Loads existing config or creates new one
results = configure_from_browser("firefox", cfg)
for platform, ok, msg in results:
status = "✅" if ok else "❌"
print(f"{status} {platform}: {msg}")
This method also syncs Twitter credentials to legacy tools via _sync_xfetch_session and _sync_bird_env where applicable.
Direct Raw Cookie Extraction with extract_all()
To retrieve cookies without updating the configuration file:
from agent_reach.cookie_extract import extract_all
cookies = extract_all("chrome")
print(cookies["twitter"]["auth_token"]) # Specific token
print(cookies["xhs"]["cookie_string"]) # Full header string
The function returns a dictionary mapping each config_key to a sub-dictionary of extracted values. For platforms where spec["cookies"] is None, the value is a concatenated string (name=value; ...) suitable for direct HTTP header usage.
Handling Dependencies and Missing Libraries
If you attempt to extract cookies without installing the required dependencies, you'll encounter:
RuntimeError: Cookie extraction requires rookiepy or browser-cookie3.
Install: pip install rookiepy (recommended)
or: pip install browser-cookie3
Install rookiepy for optimal performance, or browser-cookie3 as a fallback.
Summary
- Agent-Reach automatically extracts authentication cookies from Chrome, Firefox, Edge, Brave, and Opera via
agent_reach/cookie_extract.py - The system uses
PLATFORM_SPECSto filter cookies by domain and name for Twitter/X, XiaoHongShu, Bilibili, and Xueqiu - Implementation tries
rookiepyfirst, then falls back tobrowser_cookie3, raising a clear error if neither is present - Use
agent-reach configure --from-browser <browser>for CLI-based extraction orconfigure_from_browser()for programmatic access - Extracted credentials are stored in
~/.agent-reach/config.yamland optionally synced to legacy tool environments
Frequently Asked Questions
Which browsers does Agent-Reach support for cookie extraction?
Agent-Reach supports Chrome, Firefox, Edge, Brave, and Opera. The browser name is normalized to lowercase and validated against supported backends in agent_reach/cookie_extract.py.
What Python libraries are required to extract cookies from Chrome or Firefox?
You must install either rookiepy (recommended, Rust-based) or browser_cookie3 (Python-based). The code attempts to import rookiepy first; if unavailable, it falls back to browser_cookie3. If neither is found, a RuntimeError prompts you to install one.
How does Agent-Reach handle different cookie formats for various platforms?
The PLATFORM_SPECS dictionary in agent_reach/cookie_extract.py defines platform-specific rules. For Twitter/X and Bilibili, it extracts specific named cookies like auth_token and SESSDATA. For XiaoHongShu and Xueqiu, it concatenates all domain cookies into a single cookie_string formatted as name=value; name2=value2.
Where does Agent-Reach store extracted browser cookies?
Cookies are written to the Agent-Reach configuration file at ~/.agent-reach/config.yaml. The configure_from_browser() function in agent_reach/cookie_extract.py handles this integration, optionally syncing Twitter credentials to legacy tools like xfetch and bird CLI environments.
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:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →