huehnerlady
07/28/2020, 3:10 PMAuthorization
header manually works, but I need to automate it using clientCredentials flow and client-id and client-secret.
So far when I google about it, I just get how to set up oauth with ktor on the server-side, not on the client side.
Has anybody done so and can give me a code example or a link where I can find help?Nikky
07/28/2020, 3:56 PMhuehnerlady
07/29/2020, 6:03 AMNikky
07/29/2020, 7:23 AMhuehnerlady
07/29/2020, 7:28 AMimport io.ktor.client.HttpClient
import io.ktor.client.engine.apache.Apache
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.request.accept
import io.ktor.client.request.forms.FormDataContent
import io.ktor.client.request.header
import <http://io.ktor.client.request.post|io.ktor.client.request.post>
import io.ktor.http.ContentType
import io.ktor.http.parametersOf
import io.ktor.utils.io.core.toByteArray
import kotlinx.coroutines.runBlocking
import java.util.*
var token: String = ""
private val clientId: String? = System.getenv("CLIENT_ID")
private val clientSecret: String? = System.getenv("CLIENT_SECRET")
private val client = HttpClient(Apache) {
install(JsonFeature)
}
fun getAccessToken(): String {
if (token.isBlank()) {
retrieveToken()
}
return token
}
fun retrieveToken(): String {
val data = runBlocking {
<http://client.post|client.post><Map<String, String>>("<access-token-endpoint>") {
header("Authorization", constructBasicAuthValue())
accept(ContentType.Application.Json)
body = FormDataContent(parametersOf("grant_type" to listOf("client_credentials")))
}
}
token = data["access_token"] ?: throw IllegalStateException("Error retrieving access token")
return token
}
private fun constructBasicAuthValue(): String {
val authString = "$clientId:$clientSecret"
val authBuf = Base64.getEncoder().encodeToString(authString.toByteArray())
return "Basic $authBuf"
}
For me for some reason the Auth
Feature did not work, so I copy/pasted the function in the bottom from there.
I basically created an Extensions file and added those methods there. not yet polished, but maybe good enough for now 🙂