ktor中如何使用refershToken
How to use refershToken in ktor
嘿,我在 kotlin multiplatform moblie 工作。我想问如果api returns 401状态,我想调用refresh api。我正在阅读 configure ktor 的文档,但无法理解这一点。有人可以指导我吗?我在我这边尝试了一些代码,有人可以指导我如何在我的解决方案中实现任何适当的示例。
commonMain
expect fun httpClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient
iOSMain
actual class Platform actual constructor() {
actual val versionCode =
platform.Foundation.NSBundle.mainBundle.infoDictionary?.get("CFBundleVersion").toString()
actual val accessToken = ""
}
androidMain
actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(OkHttp) {
config(this)
install(ContentNegotiation) {
json(Json {
prettyPrint = true
ignoreUnknownKeys = true
explicitNulls = false
})
}
engine {
config {
retryOnConnectionFailure(true)
connectTimeout(30, TimeUnit.SECONDS)
readTimeout(40, TimeUnit.SECONDS)
}
}
defaultRequest {
header("Client-Version", Platform().versionCode)
}
HttpResponseValidator {
validateResponse { response ->
when (response.status.value) {
401 -> {
}
}
}
}
install(Auth) {
bearer {
loadTokens {
BearerTokens(tokenProvider.accessToken, "")
}
}
}
}
Platform.kt
package com.example.kotlinmultiplatformsharedmodule
lateinit var provider: VersionAndroidProvider
lateinit var tokenProvider: AndroidToken
actual class Platform actual constructor() {
actual val versionCode get() = provider.version
actual val accessToken: String
get() = tokenProvider.accessToken
}
interface VersionAndroidProvider {
val version: String
}
interface AndroidToken {
val accessToken: String
}
- I need to call api, if api returns 401 status, I need to call refershToken api. After getting new accessToken from refreshToken api, I need to send this to api call.
- If refreshToken is giving 401 then I need to infrom my application to logout.
如果您在 Ktor 的身份验证插件中使用 Bearer 提供程序,那么 refreshTokens
lambda 将在服务器 returns 401 时被调用。有关更多信息,请阅读 documentation。这是您的用例的不完整示例:
val client = HttpClient(Apache) {
install(Auth) {
bearer {
loadTokens {
BearerTokens("initial_access_token", "initial_refresh_token")
}
refreshTokens {
val response = client.get("https://example.com/get_token")
if (response.status == HttpStatusCode.Unauthorized) {
// logout
null
} else {
// get token from a response
BearerTokens("new_access_token", "new_refresh_token")
}
}
}
}
}
嘿,我在 kotlin multiplatform moblie 工作。我想问如果api returns 401状态,我想调用refresh api。我正在阅读 configure ktor 的文档,但无法理解这一点。有人可以指导我吗?我在我这边尝试了一些代码,有人可以指导我如何在我的解决方案中实现任何适当的示例。
commonMain
expect fun httpClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient
iOSMain
actual class Platform actual constructor() {
actual val versionCode =
platform.Foundation.NSBundle.mainBundle.infoDictionary?.get("CFBundleVersion").toString()
actual val accessToken = ""
}
androidMain
actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(OkHttp) {
config(this)
install(ContentNegotiation) {
json(Json {
prettyPrint = true
ignoreUnknownKeys = true
explicitNulls = false
})
}
engine {
config {
retryOnConnectionFailure(true)
connectTimeout(30, TimeUnit.SECONDS)
readTimeout(40, TimeUnit.SECONDS)
}
}
defaultRequest {
header("Client-Version", Platform().versionCode)
}
HttpResponseValidator {
validateResponse { response ->
when (response.status.value) {
401 -> {
}
}
}
}
install(Auth) {
bearer {
loadTokens {
BearerTokens(tokenProvider.accessToken, "")
}
}
}
}
Platform.kt
package com.example.kotlinmultiplatformsharedmodule
lateinit var provider: VersionAndroidProvider
lateinit var tokenProvider: AndroidToken
actual class Platform actual constructor() {
actual val versionCode get() = provider.version
actual val accessToken: String
get() = tokenProvider.accessToken
}
interface VersionAndroidProvider {
val version: String
}
interface AndroidToken {
val accessToken: String
}
- I need to call api, if api returns 401 status, I need to call refershToken api. After getting new accessToken from refreshToken api, I need to send this to api call.
- If refreshToken is giving 401 then I need to infrom my application to logout.
如果您在 Ktor 的身份验证插件中使用 Bearer 提供程序,那么 refreshTokens
lambda 将在服务器 returns 401 时被调用。有关更多信息,请阅读 documentation。这是您的用例的不完整示例:
val client = HttpClient(Apache) {
install(Auth) {
bearer {
loadTokens {
BearerTokens("initial_access_token", "initial_refresh_token")
}
refreshTokens {
val response = client.get("https://example.com/get_token")
if (response.status == HttpStatusCode.Unauthorized) {
// logout
null
} else {
// get token from a response
BearerTokens("new_access_token", "new_refresh_token")
}
}
}
}
}