Skip to content

Instantly share code, notes, and snippets.

@orcchg
Created October 21, 2023 08:51
Show Gist options
  • Save orcchg/1d6b327c20f2356e8b3108e90d24de06 to your computer and use it in GitHub Desktop.
Save orcchg/1d6b327c20f2356e8b3108e90d24de06 to your computer and use it in GitHub Desktop.
Interceptor with Authorization header and asynchronously obtained access token
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Response
class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val accessToken = runBlocking { obtainAccessToken() }
val request = chain.request()
val newRequest = request.newBuilder()
.addHeader("Authorization", accessToken)
.build()
return chain.proceed(newRequest)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment