Question: I am trying to implement the token based authentication with access and refresh token. The access token expires after some limited time. Then using the refresh token it needs to be updated again. I am following this [reference](https://github.com/square/okhttp/wiki/Recipes#handling-authentication) and this [answer](https://stackoverflow.com/a/31624433/8040950) as well. As I am new to android I got no idea about how to implement these concepts.

This is my interface to get access token using the refresh token.

```
  //get access Token with Refresh Token
    @POST("/api/token")
    Call<ResponseBody> getAccessToken(@Body JSONObject jsonObject);

```

> 
> This is my Retrofit Client:
> 
> 
> 

```
public class ApiClient {
        public static final String BASE_URL = "https://lit-cove-70675.herokuapp.com";
        private static Retrofit retrofit = null;
          public static Retrofit getClient() {
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit;
        }
    }

```

I have no idea where and how to implement those `Service Generators` and `Authenticator`. I have to pass `"Authentication"=>ACCESSTOKEN` in header with every API URL.

Answer: 
```
public class ApiClient {
    public static final String BASE_URL = "https://lit-cove-70675.herokuapp.com";

    public <Service> Service buildApi(Class<Service> service, String accessToken) {

        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(getOkkHttpClient(accessToken))
                .build()
                .create(service);
    }

    private OkHttpClient getOkkHttpClient(String accessToken) {

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(@NonNull Interceptor.Chain chain) throws IOException {
                Request original = chain.request();
                Request.Builder requestBuilder = original.newBuilder();

                // ============= Your access token will go here ============
                if(accessToken != null) {
                   requestBuilder.header("Access token key", accessToken);
                }

                return chain.proceed(requestBuilder.build());
            }
        });
        return httpClient.build();
    }
}

```
Please use below code to create your service:

```
public <S> S createService(Class<S> serviceClass, AuthToken authToken) {

            final String token = "Bearer" + " " + authToken.getAceessToken();
            Log.d("ServiceGenerator", "Token:::::::  " + token);

            httpClient.addInterceptor(chain -> {
                Request original = chain.request();

                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder()
                        .header(KEY_AUTH_HEADER, token).header("Content-Type", "application/json");

                Request request = requestBuilder.build();
                return chain.proceed(request);
            });

            Retrofit retrofit = builder.client(httpClient.build()).build();
            return retrofit.create(serviceClass);
        }

```

Add following line to call method:

```
 ApiService service = serviceGenerator.createService(ApiService.class, token);
        Call<ResponseBody> call = service.getMovielist(id);

```

serviceGenerator is the class where i have defined create service method.You can declare whenever you want.