I found the answer!
Basically I need an interceptor, as usual, and I had to check the URL there to see if I should add an authorization header or not.
import java.io.IOException; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; public class NetInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (request.url().encodedPath().equalsIgnoreCase("/oauth/token") || (request.url().encodedPath().equalsIgnoreCase("/api/v1/users") && request.method().equalsIgnoreCase("post"))) { return chain.proceed(request); } Request newRequest = request.newBuilder() .addHeader("Authorization", "Bearer token-here") .build(); Response response = chain.proceed(newRequest); return response; } }
source share