30 lines
1.1 KiB
Kotlin
30 lines
1.1 KiB
Kotlin
package dev.fyloz.musicplayer.modules.spotify
|
|
|
|
import dev.fyloz.musicplayer.modules.spotify.api.AccessTokenRequest
|
|
import dev.fyloz.musicplayer.modules.spotify.api.AccessTokenResponse
|
|
import io.ktor.client.*
|
|
import io.ktor.client.call.*
|
|
import io.ktor.client.request.*
|
|
import io.ktor.http.*
|
|
import io.ktor.util.*
|
|
import org.koin.core.component.KoinComponent
|
|
import org.koin.core.component.inject
|
|
|
|
class SpotifyTokenLogic : KoinComponent {
|
|
private val httpClient by inject<HttpClient>()
|
|
|
|
suspend fun getAccessToken(authorizationCode: String) {
|
|
val authorizationHeader = "1372bd3ebcad4f889994f9a3f675472b:26ac249dc5ca4a309aa08f8cfcec9a60".encodeBase64()
|
|
|
|
val call = httpClient.post("https://accounts.spotify.com/api/token") {
|
|
contentType(ContentType.Application.FormUrlEncoded)
|
|
accept(ContentType.Application.Json)
|
|
setBody(AccessTokenRequest(authorizationCode, "http://localhost:8080/module/spotify/login-callback"))
|
|
header("Authorization", "Basic $authorizationHeader")
|
|
}
|
|
|
|
val response = call.body<AccessTokenResponse>()
|
|
println(response.accessToken)
|
|
}
|
|
}
|