https://kotlinlang.org logo
#ktor
Title
# ktor
r

Raman Sharma

10/31/2023, 8:39 AM
Hello Everyone, I am using below block to reauthenticate if token expires. Here for the original request where I got 401, once the token is updated, I want to add one more parameter to original request.But I am not able to find a way to it. Any help will be appreciated. install(Auth) { bearer { refreshTokens { // this: RefreshTokensParams // Refresh tokens and return them as the 'BearerTokens' instance val refreshToken = CoreSDK.get("refreshToken","") val apiUrl = CoreSDK.baseUrl + "/oauth/token" val response = client.post(apiUrl) { markAsRefreshTokenRequest() basicAuth(Constants.USER_NAME, Constants.PASSWORD) setBody( FormDataContent( Parameters.build { append("refresh_token", refreshToken) append("grant_type", "refresh_token") } ) ) } var token:RefreshToken ?= null if(response.isSuccessful()){ val bodyAsText = response.bodyAsText() token = jsonParser.decodeFromString(bodyAsText) CoreSDK.save("tokenValue",token?.access_token.orEmpty()) CoreSDK.save("refreshToken",token?.refresh_token.orEmpty()) } BearerTokens( accessToken = token?.access_token.orEmpty(), refreshToken = token?.refresh_token.orEmpty() ) } } }
a

Aleksei Tirman [JB]

10/31/2023, 9:21 AM
Unfortunately, that's not possible.
r

Raman Sharma

10/31/2023, 9:35 AM
@Aleksei Tirman [JB] what is the standard approach?
a

Aleksei Tirman [JB]

10/31/2023, 9:40 AM
For what?
r

Raman Sharma

10/31/2023, 9:41 AM
Refresh token
so the above code snippet is correct for handling refresh token if any api gets 401
a

Aleksei Tirman [JB]

10/31/2023, 9:42 AM
Yes. It seems correct.