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. 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.
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
Tests if a previously captured session is still active. Opens a browser with the saved session so you can verify logins are still valid.
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.pypython3 --versionIf not installed, download from https://python.org or use Homebrew:
brew install pythonpython3 -m venv venv
source venv/bin/activatepip install -r requirements.txtsource venv/bin/activate
python capture_browser_session.pyA 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.
To verify your session is still active:
source venv/bin/activate
python test_session.pyThis 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- Go to your Actor's input page in Apify Console
- Find the "Browser Session" field
- Upload your
browser_session.jsonfile
- Local only: Run this script on your own computer, never on shared systems
- Encrypted storage: Apify's
isSecretfields use AES-256-GCM encryption - Session expiry: Sessions typically last 2-4 weeks; regenerate if actors fail to authenticate
- Don't commit:
browser_session.jsonis in .gitignore for safety
Use a virtual environment (see Setup step 2).
Ensure you're not running in a headless environment (SSH, Docker).
- Try regenerating the session - it may have expired
- Some sites may detect different IP/location; try again
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.