Skip to content

mrieck/browser-session-capture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Browser Session Capture

Capture browser sessions locally for use with Apify actors that require OAuth or platform authentication.

Why This Exists

Some websites only allow login via OAuth (Google, Microsoft, Discord) and don't offer email/password signup. Other times you want to post to platforms where you already have an account (Reddit, Twitter, HackerNews). Since Apify actors run in the cloud without interactive access, you need to capture your authenticated session locally and upload it to Apify.

Scripts

capture_browser_session.py

Captures your browser session (cookies + localStorage) for all platforms you log into.

Use cases:

  • Google OAuth for signup-based sites
  • Reddit for posting to subreddits
  • Twitter, HackerNews, or any platform with existing accounts

test_session.py

Tests if a previously captured session is still active. Opens a browser with the saved session so you can verify logins are still valid.

Quick Start

git clone https://github.com/YOUR_USERNAME/browser-session-capture.git
cd browser-session-capture
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python capture_browser_session.py

Setup

1. Install Python (if needed)

python3 --version

If not installed, download from https://python.org or use Homebrew:

brew install python

2. Create Virtual Environment

python3 -m venv venv
source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

Usage

Capturing a Session

source venv/bin/activate
python capture_browser_session.py

A browser window will open. Log into any platforms you need:

  • Google (for OAuth signups) - accounts.google.com
  • Reddit (for subreddit posting) - reddit.com
  • Twitter, HackerNews, etc. - navigate to each and log in

You can open multiple tabs and log into as many platforms as you want.

Return to the terminal and press Enter. The script saves your session to browser_session.json.

Testing a Session

To verify your session is still active:

source venv/bin/activate
python test_session.py

This opens a browser with your saved session and navigates to Google account page so you can verify you're still logged in. Sessions typically expire after 2-4 weeks.

You can also specify a different session file:

python test_session.py my_other_session.json

Using the Session in Apify

  1. Go to your Actor's input page in Apify Console
  2. Find the "Browser Session" field
  3. Upload your browser_session.json file

Security Notes

  • Local only: Run this script on your own computer, never on shared systems
  • Encrypted storage: Apify's isSecret fields use AES-256-GCM encryption
  • Session expiry: Sessions typically last 2-4 weeks; regenerate if actors fail to authenticate
  • Don't commit: browser_session.json is in .gitignore for safety

Troubleshooting

"externally-managed-environment" error

Use a virtual environment (see Setup step 2).

Browser doesn't open

Ensure you're not running in a headless environment (SSH, Docker).

Session doesn't work in Apify

  • Try regenerating the session - it may have expired
  • Some sites may detect different IP/location; try again

File Output

The script creates browser_session.json containing:

{
  "cookies": [...],
  "origins": [...]
}

This is the Playwright/Camoufox storage state format containing all cookies and localStorage for every platform you logged into during the capture session.

About

Capture browser sessions locally for use with Apify actors that require OAuth or platform authentication. Some websites only allow login via OAuth (Google, Microsoft, Discord) and don't offer email/password signup. If you want to automate them with agent this will save your sessions for use in Camoufox.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages