I'm having basically the same problem as the user asking this question: AppAuth library for Android lacks proper documentation.
My problem occurs when the current access token expires, disallowing API communication from inside my app. To prevent that I forced my TokenInterceptor
to acquire the token on each request from the getAccessToken
method, which uses AppAuth's performActionWithFreshTokens
method, that supposedly performs a token refresh request (I looked through its code). However, it always throws an AuthorizationException
with error invalid_grant
for me.
It crashes my app the first time, but works fine after relaunching. So the token does refresh, doesn't it?
class TokenInterceptor @Inject constructor(
private val authStateStorage: AuthStateStorage,
private val authService: AuthorizationService
): Interceptor {
private companion object {
const val TAG = "TokenInterceptor"
const val AUTH_HEADER = "Authorization"
}
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
request.header(AUTH_HEADER) ?: run {
request = chain.request()
.newBuilder()
.addHeader(AUTH_HEADER, "Bearer ${getAccessToken()}")
.build()
}
return chain.proceed(request)
}
private fun getAccessToken(): String = runBlocking {
val authState = authStateStorage.authStateFlow.first()
val isNeedToUpdateToken = authState.needsTokenRefresh
// authState.refreshToken is not null or empty for me!
suspendCoroutine { continuation ->
authState.performActionWithFreshTokens(authService) { accessToken, _, exception ->
exception?.let {
Log.e(TAG, "Exception in token process: ", it)
continuation.resumeWithException(it)
} ?: run {
if (isNeedToUpdateToken) {
runBlocking {
authStateStorage.updateAuthState(authState)
}
}
continuation.resume(accessToken!!)
}
}
}
}
}
Did I forget some steps for this to work properly? Why does it throw an exception, but I still wind up with a valid token?