Authentication¶
pyhood handles Robinhood's OAuth2 authentication with three layers of session management:
- Cached session — Reuse a valid stored token (instant)
- Token refresh — Exchange a refresh token for new credentials (no human needed)
- Full login — Username + password + device approval (requires phone)
When you call pyhood.login(), it tries each layer in order, falling back only when necessary.
Login Flow¶
import pyhood
session = pyhood.login(
username="you@email.com",
password="your_password",
timeout=90,
)
What happens internally:
- Check
~/.pyhood/session.jsonfor a cached token - If found, validate it against Robinhood's API
- If expired, try refreshing with the stored refresh token
- If refresh fails, perform a full login with device approval
Token Refresh¶
This is pyhood's killer feature. The refresh token lets you renew your session without any human interaction:
Internally, this:
- Sends the stored
refresh_tokento Robinhood's OAuth endpoint - Receives a new
access_tokenand newrefresh_token - Saves both to
~/.pyhood/session.json - Invalidates the old tokens (they rotate on each refresh)
Use pyhood.refresh() in cron jobs and automation
Since refresh requires no credentials and no device approval, it's the ideal entry point for unattended scripts. Only fall back to pyhood.login() if refresh raises TokenExpiredError.
Token Lifetime¶
| Token | Observed Lifetime | Notes |
|---|---|---|
| Access token | 5-8 days | Robinhood sets this server-side; the expires_in parameter in the login request is ignored |
| Refresh token | Unknown (weeks+) | Expires eventually; triggers TokenExpiredError |
Note
Robinhood's token lifetimes are not documented and may change. pyhood handles expiration gracefully regardless of the actual lifetime.
Device Approval¶
On first login (or when the refresh token expires), Robinhood requires device approval:
- pyhood sends your credentials to Robinhood
- Robinhood pushes a notification to your phone
- You tap "Yes, it's me" in the Robinhood app
- pyhood detects the approval and completes login
Device approval is phone app only — it's not available through the web interface.
Timeouts¶
The timeout parameter controls how long pyhood waits for approval:
If you don't approve in time, pyhood raises LoginTimeoutError.
Session Storage¶
Tokens are stored in ~/.pyhood/session.json:
{
"access_token": "eyJ...",
"token_type": "Bearer",
"refresh_token": "abc...",
"device_token": "12345-6789-...",
"saved_at": 1710600000.0
}
device_tokenis preserved across sessions to minimize re-verificationsaved_attracks when tokens were last written- File permissions are your OS defaults — secure your machine accordingly
Logout¶
To revoke tokens and delete stored credentials:
This clears the session, removes ~/.pyhood/session.json, and attempts to revoke the token server-side.
MFA / Two-Factor Authentication¶
If your Robinhood account uses SMS or email-based MFA: