【发布时间】:2022-01-19 16:42:58
【问题描述】:
我正在尝试让 Ktor 客户端刷新 Kotlin 多平台项目中的 Bearer 令牌。
有一个示例 here 它应该如何工作。
我的 http 客户端配置代码看起来非常相似 - 除了获取和刷新令牌的不同请求:
...
install(Auth) {
lateinit var tokenInfo: TokenInfo
var refreshTokenInfo: TokenInfo
bearer {
loadTokens {
val url = "https://${environment.host}:${environment.port}/oauth/login"
tokenInfo = tokenClient.post<TokenInfo>(url) {
contentType(ContentType.Application.Json)
body = buildJsonObject {
put("username", "blah")
put("password", "blub")
}
}
BearerTokens(
accessToken = tokenInfo.data.access_token,
refreshToken = tokenInfo.data.refresh_token
)
}
refreshTokens {
val url = "https://${environment.host}:${environment.port}/oauth/refresh"
refreshTokenInfo = tokenClient.get<TokenInfo>(url) {
contentType(ContentType.Application.Json)
header(HttpHeaders.Authorization, tokenInfo.data.refresh_token)
}
BearerTokens(
accessToken = refreshTokenInfo.data.access_token,
refreshToken = refreshTokenInfo.data.access_token
)
}
}
}
但这会导致mutation attempt of frozen kotlin.native.internal.Ref。
好像不喜欢lateinit var tokenInfo: TokenInfo。
这只发生在 iOS 上。 Android 工作正常。
(顺便说一句,我切换到new memory model。但在这种情况下,这似乎无关紧要,这是同样的错误。)
【问题讨论】:
-
我在 iOS 中遇到了一个非常相似的问题,我已经在这里发布了一个问题和答案:- stackoverflow.com/q/69800289/12768366
-
查看this answer。新的内存模型应该可以解决这个问题,但它还没有发布,所以到目前为止可能还没有涵盖这种情况。
-
这里是关于
InvalidMutabilityException的票youtrack.jetbrains.com/issue/KTOR-1628。
标签: kotlin kotlin-multiplatform ktor kotlin-multiplatform-mobile kmm