Google Cookie Retrieval
This is a simple Selenium script used to quickly run through the Mautrix Google Chat Authentication Process.
It was written primarily with goose (honk!).
The goal: Automate this
-
Open a private chat with the bridge bot. Usually @googlechatbot:your.server.
- If the bot doesn't accept the invite, see the troubleshooting page
-
Open https://chat.google.com in a private browser window and log in normally, then extract cookies:
- Press F12 to open developer tools.
- select the "Application" (Chrome) or "Storage" (Firefox) tab.
- In the sidebar, expand "Cookies" and select https://chat.google.com.
- In the cookie list, find the COMPASS, SSID, SID, OSID and HSID rows.
- When using Firefox, you may have multiple COMPASS cookies with different paths. Pick the one where path is /.
- Form a JSON object with the extracted cookies. It should look something like this (field names are case-insensitive):
{ "compass": "dynamite-ui=...", "ssid": "...", "sid": "...", "osid": "...", "hsid": "..." }- Close the browser window to prevent the cookies being invalidated (Google uses refresh tokens, so you need to close the window within a few minutes).
-
Send login-cookie {the json object} to the bot — or let the script do it for you with
--send(see "Automating step 3" below).
Recent chats should now get portals automatically. Other chats will get portals as you receive messages.
Persistent profile & scheduled refresh
By default the tool runs Chrome in incognito and logs in fresh every
run (it needs rbw-agent unlocked and, for automated 2FA, a TOTP secret in
the rbw entry). That's the right mode for an interactive, one-off extraction.
For unattended/scheduled runs you don't want to log in from scratch each time (rbw may be locked, 2FA can't be done headlessly, and Google may challenge). Instead, use a persistent Chrome profile so the Google session survives between runs and can be refreshed headlessly:
# 1. One-time, interactively: seed the profile with a real login.
# (Run on a desktop with a display and rbw-agent unlocked.)
google-cookie-retrieval --profile-dir
# 2. Later, on a schedule: refresh the cookies headlessly.
# If the saved session is still valid, login is skipped entirely and
# rbw is never touched.
google-cookie-retrieval --profile-dir --headless --copy
--profile-dir takes an optional path. Given without a value it uses the
conventional location
$XDG_STATE_HOME/google-cookie-retrieval/profile
(or ~/.local/state/google-cookie-retrieval/profile). You can pass an
explicit path to keep multiple accounts separate, e.g.
--profile-dir ~/.local/state/google-cookie-retrieval/profile-other.
When a profile is in use, Chrome runs with --user-data-dir and not
--incognito (the two are incompatible).
What happens when the session expires
Google sessions eventually expire. When --profile-dir is set and the saved
session is no longer authenticated, the tool tries to re-seed the profile
by running the full rbw login + TOTP flow into the persistent profile (so the
next headless run works again). If that can't complete headlessly — e.g. rbw
is locked, login fails, or you pass --no-login-fallback — it exits with a
distinct non-zero code 2 and a clear message, so a scheduler/cron can alert
a human to re-run the tool interactively.
--no-login-fallback is useful for pure-refresh cron runs that should only
succeed when the session is already valid and otherwise fail loudly:
google-cookie-retrieval --profile-dir --headless --no-login-fallback --copy
# exits 2 if the session expired, instead of attempting a login
Run modes summary
| Flags | Mode | Login | rbw needed |
|---|---|---|---|
| (none) | incognito, fresh | rbw auto-login (+TOTP) | yes |
--manual |
incognito, fresh | manual (headed) | no |
--profile-dir |
persistent profile | skip if session valid; else rbw re-seed | only on re-seed |
--profile-dir --headless |
persistent profile, headless refresh | skip if valid; else re-seed (may fail → exit 2) | only on re-seed |
--profile-dir --headless --no-login-fallback |
pure refresh | skip if valid; else exit 2 | never |
Note: cookies are only ever printed to stdout (and optionally copied to the clipboard with
--copy). They are never written to disk beyond the browser's own profile storage.
Automating step 3 — pushing the cookie to the bot over Matrix
The script can now perform step 3 ("send login-cookie {json} to the bot") for
you over the Matrix client-server API, so you no longer have to paste the
cookie JSON into a Matrix client by hand. The browser is closed first (so the
Google cookies aren't invalidated while the send is in flight) and then the
login-cookie {compact-json} message is PUT to a direct room with the bridge
bot. The script then long-polls /sync for up to ~60 seconds and prints the
bot's reply (success or error text).
Cluster defaults
These are hardcoded as defaults but kept flag-overridable:
- Homeserver:
https://matrix.ogle.fyi(server nameogle.fyi) - Bridge bot MXID:
@googlechatbot:ogle.fyi
Matrix access token
The push needs a Matrix access token for your own MXID (the user that DMs the bot). It is resolved in this order:
- The
MATRIX_ACCESS_TOKENenvironment variable (takes precedence). - The
rbwentrymatrix-john-ogle-fyi(viarbw get matrix-john-ogle-fyi). The token must be stored as the entry's password field.
You can obtain such a token from the homeserver's /login endpoint (e.g. with
curl against /_matrix/client/v3/login using your username/password), or
from Element's "Help & About → Access Token" panel. The token is sent as a
Bearer token; the script validates it up front via
GET /_matrix/client/v3/account/whoami and fails fast with a clear message on
a 401 (invalid/expired token).
New CLI flags
--send— after extraction, push the cookie JSON to the bridge bot over Matrix (the manual "send to bot" step is skipped).--dry-run— print the Matrix room ID (if resolvable) and the exactlogin-cookie {json}message body that would be sent, but do not send. Works standalone without a Matrix token (room ID is resolved only ifMATRIX_ACCESS_TOKENis already set; the message body is always printed).--homeserver URL— override the homeserver base URL.--bot-mxid MXID— override the bridge bot MXID.
Examples
# Extract cookies and push them to the bridge bot:
google-cookie-retrieval --send --copy
# Dry run: show the message body / room without sending anything:
google-cookie-retrieval --dry-run --manual
# Push to a non-default homeserver/bot:
google-cookie-retrieval --send \
--homeserver https://matrix.ogle.fyi \
--bot-mxid @googlechatbot:ogle.fyi
# Provide the token via env var instead of rbw:
MATRIX_ACCESS_TOKEN=syt_... google-cookie-retrieval --send
How the DM room is chosen
The script reads the m.direct account data event to find an existing direct
room with the bot MXID. If none exists, it creates one
(POST /createRoom with invite=[bot], is_direct=true,
preset=trusted_private_chat) and uses that.
What can fail
- No bot reply within 60s is reported as a warning, not a failure — the message was still sent; check the bridge logs.
- 401 from whoami — the token is invalid/expired; refresh it and try again.
- rbw locked / entry missing — either unlock rbw (
rbw unlock) and ensure thematrix-john-ogle-fyientry exists, or setMATRIX_ACCESS_TOKEN. - Network errors — every HTTP call has a 30s timeout; the error message names the endpoint and the underlying failure.