SpotifyAuthManager

class SpotifyAuthManager(clientId: String, clientSecret: String? = null, redirectUri: String? = null, authorizationApis: AuthorizationApis = AuthorizationApis(), authorizationUriLauncher: (String) -> Boolean = ::launchAuthorizationUriOnPlatform)

High-level authorization manager for Spotify Accounts flows.

Handles PKCE state/verifier lifecycle, code exchange, token refresh, and token caching.

Constructors

Link copied to clipboard
constructor(clientId: String, clientSecret: String? = null, redirectUri: String? = null, authorizationApis: AuthorizationApis = AuthorizationApis(), authorizationUriLauncher: (String) -> Boolean = ::launchAuthorizationUriOnPlatform)

Functions

Link copied to clipboard
fun buildAuthorizationCodeUri(scope: List<String> = emptyList(), state: String? = null, showDialog: Boolean? = null): String

Builds the Spotify authorization URL for Authorization Code flow.

Link copied to clipboard
fun buildAuthorizationCodeUriAndLaunch(scope: List<String> = emptyList(), state: String? = null, showDialog: Boolean? = null): String

Builds and launches the Spotify authorization URL for Authorization Code flow.

Link copied to clipboard
fun buildAuthorizationCodeWithPkceUri(codeChallenge: String, codeChallengeMethod: String = "S256", scope: List<String> = emptyList(), state: String? = null, showDialog: Boolean? = null): String

Builds the Spotify authorization URL for Authorization Code with PKCE flow.

fun buildAuthorizationCodeWithPkceUriAndLaunch(codeChallenge: String, codeChallengeMethod: String = "S256", scope: List<String> = emptyList(), state: String? = null, showDialog: Boolean? = null): String

Builds and launches the Spotify authorization URL for PKCE flow.

Link copied to clipboard

Clears the currently stored token and expiration state.

Link copied to clipboard
suspend fun completePkceAuthorization(code: String, returnedState: String): TokenResponse

Completes PKCE authorization by exchanging the authorization code.

Link copied to clipboard

Completes PKCE authorization by parsing the Spotify callback URI and exchanging the code.

Link copied to clipboard

Exchanges an authorization code for tokens and stores the token set.

Link copied to clipboard

Exchanges an authorization code for tokens using PKCE and stores the token set.

Link copied to clipboard

Returns the currently stored token response.

Link copied to clipboard
suspend fun getValidAccessToken(leewaySeconds: Int = 60, autoRefresh: Boolean = true): String

Returns a valid access token, refreshing it when necessary.

Link copied to clipboard

Launches the Spotify authorization URL using in-app auth UI or browser.

Link copied to clipboard

Refreshes the current access token using the stored refresh token.

Link copied to clipboard

Requests a client-credentials token and stores the token set.

Link copied to clipboard
fun startPkceAuthorization(scope: List<String> = emptyList(), showDialog: Boolean? = null): PkceAuthorizationRequest

Starts Authorization Code with PKCE flow and returns authorization request data.

Link copied to clipboard
fun startPkceAuthorizationAndLaunch(scope: List<String> = emptyList(), showDialog: Boolean? = null): PkceAuthorizationRequest

Starts Authorization Code with PKCE flow and launches the authorization URL.

Link copied to clipboard
suspend fun startPkceAuthorizationAsync(scope: List<String> = emptyList(), showDialog: Boolean? = null): PkceAuthorizationRequest

Starts Authorization Code with PKCE flow asynchronously.

Link copied to clipboard
suspend fun startPkceAuthorizationAsyncAndLaunch(scope: List<String> = emptyList(), showDialog: Boolean? = null): PkceAuthorizationRequest

Starts Authorization Code with PKCE flow asynchronously and launches the authorization URL.