A
A
Artur Abdeev2020-12-12 10:51:25
Java
Artur Abdeev, 2020-12-12 10:51:25

How to work with refresh token correctly?

I need to catch an expired token by sending a refreshToken and get new access_token and refresh_token in return.
Below I have posted the code that I am going to apply. It is not written to the end. Because I do not fully understand everything.

@FormUrlEncoded
    @POST("/api/o/token/")
    Call<TokenModel> getToken(
            @Field("username") String username,
            @Field("password") String password,
            @Field("client_id") String clientId,
            @Field("client_secret") String clientSecret,
            @Field("grant_type") String grantType
    );
    @FormUrlEncoded
    @POST("/api/o/token/")
    Call<TokenModel> getNewToken(
            @Field("refresh_token") String refreshToken,
            @Field("client_id") String clientId,
            @Field("client_secret") String clientSecret,
            @Field("grant_type") String grantType
    );

    @GET("/api/users/profile")
    Call<UserProfile> getProfile();


I put a question mark where I specifically don't understand.
I just didn't quite understand Hader. What should I put there after the comma? Everything that the server requires from me to receive a new token? (In my case it is String refreshToken, String clientId, String clientSecret, String grantType) Or not?


OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .addInterceptor(new AccessTokenInterceptor(preferences))


                .addInterceptor(new Interceptor() {
                    @NotNull
                    @Override
                    public Response intercept(@NotNull Chain chain) throws IOException {
                        Request original = chain.request();
                        Request request = original.newBuilder()
                                .header("Authorization", ?)
                                .method(original.method(), original.body())
                                .build();

                        Response response =  chain.proceed(request);
                        Log.d("MyApp", "Code : "+response.code());
                        if (response.code() == 401){
                            ?
                            return response;
                        }
                        return response;
                    }
                })
                .build();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question