getValidAccessToken

suspend fun getValidAccessToken(leewaySeconds: Int = 60, autoRefresh: Boolean = true): String

Returns a valid access token, refreshing it when necessary.

Refresh behavior depends on the grant the cached token was acquired with: PKCE and Authorization Code refresh via the stored refresh token (Spotify Accounts refresh endpoint); Client Credentials has no refresh token and reacquires a fresh token instead. A refresh or reacquisition failure is thrown as com.nubasu.spotify.webapi.wrapper.api.SpotifyTokenException (an IllegalStateException) here; on the data path (via asTokenProvider) that failure surfaces as a com.nubasu.spotify.webapi.wrapper.response.common.SpotifyResult.Failure carrying the original Spotify Accounts error instead of propagating. An invalid_grant response (refresh token revoked or expired) is not recovered automatically — the caller must run the authorization flow again.

Return

Bearer access token to use in Spotify Web API requests.

Parameters

leewaySeconds

Safety window in seconds before actual expiration when the token is treated as expired.

autoRefresh

Whether to automatically execute Refresh Token flow when the current token is missing or expired.