title: 400 error after successfully refreshing Oauth 2 Token using Retrofit2 / Kotlin in Android
body: <p>This is the code I have for the refreshing token in an android app using kotlin ad retrofit 2.</p>
  <p>The <code>gradle</code> :</p>
  <pre><code>implementation &quot;com.squareup.retrofit2:retrofit:2.9.0&quot;
  implementation &quot;com.squareup.retrofit2:converter-moshi:retrofit:2.9.0&quot;
  </code></pre>
  <p>And <code>Authenticator</code> is  :</p>
  <pre><code>class OAuthAuthenticator(
          private val refreshTokenService: Repository,
          private val sessionManager: SessionManager
      ) : Authenticator {
          @Synchronized
          override fun authenticate(route: Route?, response: Response): Request? {
              try {
          //synchronized call to refresh the token
                  val refreshTokenResponse =
                      refreshTokenService.refreshJWTToken(sessionManager.getAuthTokens())
                  val sessionDataResponseBody = refreshTokenResponse.body()
                  if (refreshTokenResponse.isSuccessful &amp;&amp; sessionDataResponseBody != null &amp;&amp; !sessionDataResponseBody.jwt.isNullOrEmpty()) {
                      sessionManager.jwtToken = sessionDataResponseBody.jwt
                      // retry request with the new tokens (I get 400 error)
                      return response.request()
                          .newBuilder()
                          .addHeader(&quot;Authorization&quot;, &quot;Bearer ${sessionManager.jwtToken}&quot;)
                          .build()
                  } else {
                      throw HttpException(refreshTokenResponse)
                  }
              } catch (throwable: Throwable) {
                  when (throwable) {
                      is HttpException -&gt; {
                          onSessionExpiration()
                          return null
                      }
                  }
              }
              return null
          }
  
          private fun onSessionExpiration() {
              sessionManager.clear()
          }
      }
  </code></pre>
  <p>This is the <code>Repository</code> class :</p>
  <pre><code>object Repository {
      fun refreshJWTToken(authTokens : AuthTokens) = RetrofitBuilder.userApi.getAuthenticationToken(authTokens).execute()
  }
  </code></pre>
  <p>This is the <code>API</code>  :</p>
  <pre><code>interface UserAPI {
      @Headers(&quot;Cache-Control: no-cache&quot;)
      @POST(AUTH_TOKENS_URL)
      fun getAuthenticationToken(
          @Header(&quot;Accept&quot;)  accept : String,
          @Header(&quot;Content-Type&quot;)  contentType : String,
          @Body params: AuthTokens
      ): Call&lt;AuthTokenResponse&gt;
  }
  </code></pre>
  <p>The <code>retrofit builder</code>:</p>
  <pre><code>init {
      val connectivityManager =
          context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
      val sessionManager = SessionManager.getInstance(context)
      val httpLoggingInterceptor =
          HttpLoggingInterceptor() 
      httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
      httpClient = OkHttpClient.Builder()
          .addInterceptor(httpLoggingInterceptor)
          .addInterceptor(ConnectivityCheckInterceptor(connectivityManager))
          .addInterceptor(AuthInterceptor(sessionManager))
          .authenticator(OAuth2Authenticator(UserRepository, sessionManager))
          .readTimeout(TIME_OUT, TimeUnit.SECONDS)
          .build()
  }
  </code></pre>
  <p><strong>Question</strong> :
  I can confirm that the code refreshes the Auth token and persists it successfully. However I get a 400 error after that. Any suggestions on what I am doing wrong?</p>