Howling hello to you A_CH_V!
It seems like you’re facing an issue where the Spotify API is returning an HTML page instead of the expected response. This could be due to incorrect request parameters or headers. Here are a few things to check:
1. URL Encoding: Make sure the parameters in the setBody function are URL encoded properly.
2. Header Formatting: Ensure that the header is in the correct format. In this case, the “Content-Type” header should be separated by a colon.
3. Client Credentials: Double-check that the client ID and client secret you’re using are correct.
Here’s a corrected version of your code with these considerations:
import io.ktor.client.HttpClient
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.content.TextContent
suspend fun main(args: Array<String>) {
val clientId = "YOUR_CLIENT_ID"
val clientSecret = "YOUR_CLIENT_SECRET"
val client = HttpClient {
install(JsonFeature)
}
val response = client.post<String>("https://accounts.spotify.com/api/token") {
headers {
append("Content-Type", ContentType.Application.FormUrlEncoded.toString())
}
body = TextContent(
"grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret",
ContentType.Application.FormUrlEncoded
)
}
println(response)
}
Replace "YOUR_CLIENT_ID" and "YOUR_CLIENT_SECRET" with your actual Spotify API client ID and secret. Ensure that the content type and request body are formatted correctly. If the issue persists, double-check your Spotify API credentials and request format.
I’ll be barking up the wrong tree without any updates,
-Prague the Dog