Skip to content

Add Spotify Web API (OAuth PKCE) support: auth, token caching, resilient client, playlist/liked-songs downloads#4

Merged
Ssenseii merged 7 commits intoSsenseii:mainfrom
TheRealAlexV:TRAV_spotify_api
Dec 18, 2025
Merged

Add Spotify Web API (OAuth PKCE) support: auth, token caching, resilient client, playlist/liked-songs downloads#4
Ssenseii merged 7 commits intoSsenseii:mainfrom
TheRealAlexV:TRAV_spotify_api

Conversation

@TheRealAlexV
Copy link
Copy Markdown
Contributor

Summary

This PR introduces a complete Spotify Web API integration (Authorization Code + PKCE) so HARMONI can load tracks directly from a user’s Spotify account (Playlists + Liked Songs) and reuse the existing per-playlist selection + download pipeline.

Key deliverables:

  • New spotify_api/ package (PKCE auth, token cache, API client with retries/paging, data normalization)
  • Downloads menu integration with a dedicated Spotify submenu and guided OAuth flow
  • New config keys + README docs for Spotify setup
  • Basic tests for PKCE helpers, token manager, and data-loader paging limits

Motivation

File-based workflows (Exportify CSV / Spotify export JSON) remain supported, but direct Web API access is the most reliable and user-friendly source of up-to-date playlist + liked-song data.

What’s Included

1) OAuth PKCE implementation (no client secret)

Dependency note:

2) Token caching + expiry

Config controls:

3) Resilient Spotify Web API client (stdlib networking)

Tunable retry keys (optional):

4) High-level data loader + normalization compatible with existing workflows

5) CLI menu integration (auth + download UI)

  • Adds Spotify submenu entry in Downloads menu: downloads_menu()
  • Spotify submenu loop: _spotify_api_menu()
    • Authenticate
    • Download from my playlists
    • Download from liked songs
    • Setup help
    • Log out (clear token cache)

OAuth UX improvements:

Download workflow integration:

6) Configuration + docs

  • Adds Spotify Web API defaults + validation schema keys in DEFAULT_CONFIG and CONFIG_SCHEMA
    • spotify_client_id, spotify_redirect_uri, spotify_scopes, spotify_cache_tokens, spotify_auto_refresh
  • README includes end-to-end setup and usage for Spotify Web API workflow: readme.md

Client ID behavior:

Testing

Security / Privacy notes

User-facing workflow

  1. Configure redirect + client id (recommended) per docs in readme.md
  2. Authenticate via the Downloads → Spotify menu (auth auto-captures loopback redirect when possible): _spotify_authenticate()
  3. Choose playlists / liked songs to load and download, then select tracks per playlist via select_songs_for_playlist()

- remove config.json file
- the configuration is now handled by environment variables
- add spotify_client_id, spotify_redirect_uri, spotify_scopes, spotify_cache_tokens, spotify_auto_refresh to config
- add spotify web api (oauth pkce) section to config schema
- validate list element types in config

feat(gitignore): add config.json to gitignore

- ignore config files to prevent credential leakage

feat(downloads_menu): integrate spotify web api (oauth pkce)

- add spotify web api menu with authentication, playlist download, and liked songs download options
- add spotify authentication using oauth pkce flow
- add playlist download from spotify web api
- add liked songs download from spotify web api
- add spotify api credential setup help
- add spotify token status display
- add spotify logout (clear cached token) option

docs(readme): update readme with spotify web api instructions

- add spotify web api feature description
- add spotify web api setup and authentication instructions
- add spotify web api authentication instructions
- add spotify web api playlist and liked songs download instructions
- update project structure with spotify api directory
- update prerequisites with spotify web api requirements

feat(spotify_api): create spotify web api package

- create spotify web api package with auth, client, data loader, and token manager modules
- add spotify authentication using oauth pkce flow
- add spotify client for making api requests
- add spotify data loader for loading playlists and liked songs
- add spotify token manager for caching and refreshing tokens

test(spotify_api): add basic tests for spotify api

- add tests for spotify auth helpers
- add tests for spotify token manager
- add tests for spotify data loader normalization

test(spotify_api): add tests for data loader limits

- add tests for spotify data loader limits
- create config.json.example with default settings
- include spotify api and sync configurations
- update spotify redirect uri to 127.0.0.1 for local development
- create spotify_tokens.json to store access tokens
- add httpx dependency for making http requests
- update authentication flow with pkce
- provide user-friendly instructions for spotify app setup
- add token management to handle token refresh and storage
- improve error handling and logging for authentication failures
- update start script to check missing dependencies and install them if needed
- add minimal local callback server for auto-capture of redirect url
- improve ux and instructions for spotify oauth (pkce) flow
- update readme and comments to reflect changes
- fix docker setup instructions
- update musicbrainz user agent
- rename trav-dj to harmoni
- add diagnostics for common auth issues (401/403)
- improve error message handling to display server-provided details
- update spotify_tokens.json with new access and refresh tokens

refactor(spotify): remove audio features

- remove audio features from spotify downloads
- remove audio features option and related code
- remove audio features from tests

docs(readme): remove audio features

- remove audio features from readme
- remove spotify_tokens.json from tracked files (I CHANGED THEM, NO LEAKY LEAKY!!!)
- add spotify_tokens.json to .gitignore
@Ssenseii Ssenseii merged commit bbffb82 into Ssenseii:main Dec 18, 2025
@Ssenseii
Copy link
Copy Markdown
Owner

I'm encountering a problem when I try to authenticate.
INVALID_CLIENT: Invalid redirect URI
either that or I configured it wrong cause I kept the Spotify client ID empty, as I didn't create a Spotify app in their developer platform.

You mind explaining how to setup the Spotify API integration so I can add it to the docs? @TheRealAlexV
Also, I'm not using the dockerized version if that makes any difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants