title: Android OkHttp, refresh expired token
body: <p><strong>Scenario</strong>: I am using OkHttp / Retrofit to access a web service: multiple HTTP requests are sent out at the same time. At some point the auth token expires, and multiple requests will get a 401 response.</p>
  
  <p><strong>Issue</strong>: In my first implementation I use an interceptor (here simplified) and each thread tries to refresh the token. This leads to a mess.</p>
  
  <pre><code>public class SignedRequestInterceptor implements Interceptor {
  
      @Override
      public Response intercept(Chain chain) throws IOException {
          Request request = chain.request();
  
          // 1. sign this request
          request = request.newBuilder()
                      .header(AUTH_HEADER_KEY, BEARER_HEADER_VALUE + token)
                      .build();
  
  
          // 2. proceed with the request
          Response response = chain.proceed(request);
  
          // 3. check the response: have we got a 401?
          if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
  
              // ... try to refresh the token
              newToken = mAuthService.refreshAccessToken(..);
  
  
              // sign the request with the new token and proceed
              Request newRequest = request.newBuilder()
                                  .removeHeader(AUTH_HEADER_KEY)
                                  .addHeader(AUTH_HEADER_KEY, BEARER_HEADER_VALUE + newToken.getAccessToken())
                                  .build();
  
              // return the outcome of the newly signed request
              response = chain.proceed(newRequest);
  
          }
  
          return response;
      }
  }
  </code></pre>
  
  <p><strong>Desired solution</strong>: All threads should wait for one single token refresh: the first failing request triggers the refresh, and together with the other requests waits for the new token.</p>
  
  <p>What is a good way to proceed about this? Can some built-in features of OkHttp (like the Authenticator) be of help? Thank you for any hint.</p>