How to Extract Cookies from Browser for Platform Authentication with Agent Reach

Agent Reach automates browser cookie extraction by scanning Chrome, Firefox, Edge, Brave, and Opera for authentication tokens and injecting them into your configuration without manual copy-pasting.

Agent Reach is an open-source automation framework that eliminates manual cookie management by programmatically extracting authentication tokens directly from your local browsers. When you need to authenticate with platforms like Twitter, XiaoHongShu, Bilibili, or Xueqiu, the tool automatically pulls the required session cookies and stores them in your configuration file. This guide explains how the cookie_extract.py module handles browser cookie extraction for platform authentication using both Python and CLI interfaces.

The extraction logic resides in agent_reach/cookie_extract.py and operates through a dual-library approach for maximum compatibility.

Supported Browsers and Backend Libraries

The helper attempts to use the Rust-based rookiepy library first for faster, more reliable access to browser storage. If rookiepy fails or is unavailable, it automatically falls back to browser-cookie3. The implementation supports Chrome, Firefox, Edge, Brave, and Opera across all major operating systems.

Agent Reach maintains a PLATFORMS mapping table that defines exactly which cookies each service requires. For example, Twitter needs auth_token and ct0, while Bilibili requires SESSDATA and bili_jct. Xueqiu has special handling—the tool only saves the cookie string when it contains xq_a_token, ignoring anonymous sessions that cannot perform authenticated actions.

The process follows a three-stage pipeline that normalizes raw browser data into ready-to-use configuration values.

Normalization and Domain Filtering

When configure_from_browser() runs, it calls browser-specific extraction functions that return raw cookie objects. These are wrapped in a lightweight _Cookie class that normalizes attributes like name, value, and domain. The wrapper iterates through each platform's domain pattern, selecting either specific named cookies from the requirements list or, when cookies is set to None, assembling all matching cookies into a semicolon-separated header string (e.g., "auth_token=xxx; ct0=yyy").

Configuration Storage and Validation

After gathering cookies, the helper writes them via config.set(key, value) into the central YAML configuration. Each platform receives a dedicated key: twitter_cookie, xhs_cookie, bilibili_cookie, or xueqiu_cookie. The Xueqiu implementation performs additional validation to ensure the presence of xq_a_token before storage, preventing invalid authentication states.

You can trigger extraction through the CLI for automated setup or programmatically via Python for custom workflows.

Command-Line Interface

Running the installation command with auto-detection imports cookies for all supported platforms:

python -m agent_reach.cli install --env=auto

Alternatively, use the diagnostic command:

agent-reach doctor

The CLI prints concise status lines (e.g., Twitter ✅ 2 cookies) and continues without aborting if a browser is locked or unavailable, issuing a warning instead of failing.

Python API Integration

Import the extraction function directly to update your configuration:

from agent_reach.cookie_extract import configure_from_browser

# Updates ~/.agent-reach/config.yml with cookies from all browsers

configure_from_browser()

Access stored cookies through the Config class:

from agent_reach.config import Config

cfg = Config()
twitter_cookie = cfg.get("twitter_cookie")      # "auth_token=abc; ct0=def"

xhs_cookie = cfg.get("xhs_cookie")              # "a=1; b=2"

bilibili_cookie = cfg.get("bilibili_cookie")
xueqiu_cookie = cfg.get("xueqiu_cookie")       # Only if xq_a_token present

For targeted extraction of specific platforms:

from agent_reach.cookie_extract import _extract_platform_cookies

# Extract only Twitter cookies from Chrome

cookies = _extract_platform_cookies(platform="twitter", browser="chrome")
print(cookies)   # {'auth_token': 'xxx', 'ct0': 'yyy'}

Summary

  • Agent Reach automatically extracts cookies from Chrome, Firefox, Edge, Brave, and Opera using agent_reach/cookie_extract.py.
  • The tool prioritizes the rookiepy library for performance, falling back to browser-cookie3 when necessary.
  • Platform-specific requirements are encoded in the PLATFORMS table, ensuring only necessary cookies (like auth_token for Twitter or SESSDATA for Bilibili) are extracted.
  • Extracted values are stored under standardized keys (twitter_cookie, xhs_cookie, etc.) in the YAML configuration via agent_reach/config.py.
  • Both CLI (install --env=auto) and Python API (configure_from_browser()) interfaces support seamless, non-interactive authentication setup.

Frequently Asked Questions

Agent Reach supports Chrome, Firefox, Edge, Brave, and Opera. The extraction logic in agent_reach/cookie_extract.py attempts to read from all these browsers simultaneously, merging results to find valid authentication tokens regardless of which browser you used to log in.

If the browser is currently running or the password store is locked, the extraction will fail for that specific browser. The CLI prints a warning message but continues processing other browsers and platforms rather than aborting the entire operation. For programmatic usage, the function handles exceptions gracefully and returns empty results for inaccessible browsers.

Why does Xueqiu require special handling compared to other platforms?

Unlike Twitter or Bilibili where specific cookie names are sufficient, Xueqiu cookies are only useful if they contain the xq_a_token value. The code in agent_reach/cookie_extract.py explicitly validates this token's presence before saving the xueqiu_cookie configuration key, preventing the storage of anonymous session data that cannot perform authenticated API requests.

Can I extract cookies for a single platform without scanning all browsers?

Yes. While configure_from_browser() processes all platforms, you can use _extract_platform_cookies(platform="twitter", browser="chrome") to target a specific platform and browser combination. This function returns a dictionary of cookie names and values without automatically updating the global configuration, giving you fine-grained control over the authentication flow.

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 →