I am trying to add NTLM authentication (username and password) to a Retrofit OkHttp client.
import java.io.IOException;
import okhttp3.Credentials;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public class NTMLAuthInterceptor implements Interceptor {
private String credentials;
public NTMLAuthInterceptor(String user, String password) {
this.credentials = Credentials.basic(user, password);
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request authenticatedRequest = request.newBuilder()
.header("Authorization", credentials).build();
return chain.proceed(authenticatedRequest);
}
}
Adding the interceptor to an OkHttp client
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new NTMLAuthInterceptor(username, password))
.build();
but in response I am getting 401 error. Can anyone help me?