0

I'm getting a 401, because token has expired, so I need to renew the token with another call and then do the call again, is there an easy way instead of doing :

disposable = loginService.login(
                UserToLoginRequest(
                    input_email_login.text.toString(),
                    input_password_login.text.toString(),
                    generateRandomDeviceInfo()
                )
            )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    { result ->
                        //It works
                    },
                    { error -> if(error.code == 401) renewAccessToken() }
                )

The thing is that I want to do something like this guy : Refreshing Oath token, but if it's possible to call again the same endpoint with the same parameters.

Example

getApple(1) <-- return info of apple id 1

The result is 401 <-- can't do the call without refreshing the accessToken refreshAccessToken()

Automatically call getApple(1) without disturbing the user

Pradip Tilala
  • 1,305
  • 10
  • 18
StuartDTO
  • 535
  • 4
  • 16
  • 38
  • 1
    Possible duplicate of [Refreshing OAuth token using Retrofit without modifying all calls](https://stackoverflow.com/questions/22450036/refreshing-oauth-token-using-retrofit-without-modifying-all-calls) – Basi Mar 13 '19 at 09:08

1 Answers1

0
class Inceptor : Interceptor {
    internal var token: String?=null
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {

        var request=chain.request()
        request=request.newBuilder().build()

        val response=chain.proceed(request)


                if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                    // get a new token (I use a synchronous Retrofit call)

                        val requestBody=FormBody.Builder()
                                .add("UserName", “abcd")
                                .add("Password", “*****")

                                .build()
                        val newRequest=request.newBuilder()
                                .url("Put your url")
                                .post(requestBody)
                                .build()
                        val tokenRefreshResponse=chain.proceed(newRequest)
                        val newRetryHttpUrl=request.url()
                        if (tokenRefreshResponse.code() == HttpURLConnection.HTTP_OK) {
                            val retryOriginaResponseBody: RequestBody

                            val builder=FormBody.Builder()
                            retryOriginaResponseBody=builder.build()
                            val retryRequest: Request
                            if (request.method() == "POST") {
                                retryRequest=request.newBuilder()
                                        .url(request.url())
                                        .post(retryOriginaResponseBody)

                                        .build()
                            } else {
                                retryRequest=request.newBuilder()
                                        .url(newRetryHttpUrl)

                                        .build()
                            }
                            val retryResponse=chain.proceed(retryRequest)
                            return retryResponse
                        } else {
                            return tokenRefreshResponse
                        }

                    }



        }
        return response
    }
}
Umesh Maharjan
  • 252
  • 2
  • 5
  • How do I recall the previous call? – StuartDTO Mar 13 '19 at 09:22
  • if (request.method() == "POST") { retryRequest=request.newBuilder() .url(request.url()) .post(retryOriginaResponseBody) .build() } else { retryRequest=request.newBuilder() .url(newRetryHttpUrl) .build() } (it will auto call the previous call) – Umesh Maharjan Mar 13 '19 at 09:25