Created
October 21, 2023 08:51
-
-
Save orcchg/1d6b327c20f2356e8b3108e90d24de06 to your computer and use it in GitHub Desktop.
Interceptor with Authorization header and asynchronously obtained access token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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