A script for automatic management of Trakt authorization tokens. Designed for use with the Elementum addon in Kodi through the Elementum Trakt Updater addon to use tokens from a single account on multiple devices. It may also be useful in other projects.
The script performs:
- Obtaining OAuth tokens via device code flow.
- Automatically refreshing the access token before it expires.
- Saving tokens to a
trakt_tokens.jsonfile. - Generating a
elementum_trakt_config.jsonconfiguration file with the access token, username, refresh token, and expiration time. - Option to hide the real refresh token in the config (a fake one is used).
Using different Client IDs and Client Secrets or moving them to settings and recompiling Elementum. If there are many devices, this may appear suspicious to Trakt's anti-fraud system.
- Authorization via device code — simply enter the code on the Trakt website.
- Automatic token refresh (checks every 5 minutes).
- One‑time mode (
--once) — perform a check and exit. - Force refresh (
--force) — forcibly refresh the token. - Force reauthorization (
--auth) — reset tokens and perform a new authorization on the Trakt website. - Hide real refresh token (
--hide-refresh-token) — a fake token is written to the Elementum config, while the real one remains intrakt_tokens.json. - Logging to
trakt_manager.logand the console.
- Python 3.6 or higher.
- The
requestslibrary (installed via pip).
Use Windows standalone executable. or
- Clone the repository or download the script.
- Install dependencies:
pip install requests
- Run the script.
python trakt_manager.pyThe script will perform initial authorization (if no tokens exist), save the config, and then check every 5 minutes whether the token needs refreshing. Press Ctrl+C to stop.
python trakt_manager.py --onceChecks the tokens, refreshes if needed, generates the config, and exits.
python trakt_manager.py --forceForcibly refreshes the access token even if the current one hasn’t expired.
python trakt_manager.py --authResets old tokens and starts the authorization procedure again.
python trakt_manager.py --username mytraktusernameIf you know the username in advance, you can provide it to avoid being prompted automatically. The username is also saved in the token file.
python trakt_manager.py --hide-refresh-tokenA fake refresh token (64 random characters) is written to elementum_trakt_config.json. The real refresh token remains only in trakt_tokens.json. This can be useful if you are sharing the config with third‑party applications and don’t want to expose the real token.
- trakt_tokens.json — stores all tokens and metadata:
{ "access_token": "...", "refresh_token": "...", "created_at": 1234567890, "expires_in": 86400, "username": "mytraktusername", "fake_refresh_token": "...", "fake_expires_in": 864000 } - elementum_trakt_config.json — configuration for Elementum:
{ "trakt_token": "access_token", "trakt_username": "mytraktusername", "trakt_refresh_token": "fake_refresh_token_or_real", "trakt_token_expiry": 1234567890 }
The script writes logs to trakt_manager.log and the console. The log level is INFO.
- The script uses standard Trakt authorization parameters pre‑registered for the Elementum addon. If you want to use your own Trakt application, change
CLIENT_IDandCLIENT_SECRETin the code. - In continuous mode, the check interval is 5 minutes. You can change it by editing
time.sleep(300)in the loop. - If the token has expired, the script tries to refresh it using the refresh_token. If refresh fails, a full authorization is performed.