I am using Android Annotations to manage my client-webservice communication.
When I try to login to an endpoint with AA I get 401 Unauthorized error, but I can successfully login to that endpoint and get a response when I use Postman.
My Rest Client code is like this:
@Rest(rootUrl = NetworkConstants.BASE_URL, converters = {GsonHttpMessageConverter.class, StringHttpMessageConverter.class},
interceptors = {RestAuthInterceptor.class})
public interface RestClient extends RestClientHeaders, RestClientErrorHandling {
@Post(NetworkConstants.OFFICER_LOGIN)
LoginModel loginOfficer(LoginModel.LoginRequest request);
}
And my Login Model is like this:
public class LoginModel {
@SerializedName("id")
public String id;
@SerializedName("ttl")
public int ttl;
@SerializedName("userId")
public int userId;
@SerializedName("created")
public Date created;
public static class LoginRequest {
@SerializedName("email")
public String email;
@SerializedName("password")
public String password;
}
}
And the url I am using is like this:
public static final String BASE_URL = "https://api.com/api/";
public static final String LOGIN = "login";
And the Interceptor class I am using is like this:
@EBean(scope = EBean.Scope.Singleton)
public class RestAuthInterceptor implements ClientHttpRequestInterceptor {
@Override
public org.springframework.http.client.ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = request.getHeaders();
HttpAuthentication auth = new HttpBasicAuthentication("emailadress", "password");
headers.setAuthorization(auth);
return execution.execute(request, body);
}
}
As I said earlier, when I use these values on Postman, I got a successful response, but when I try this within the app, I got a 401 error.
I have tried some solutions and tried to add BasicHttpAuth within the interceptor but did not work, I wonder if that is a common problem or is there a problem with the API.
Note:The API endpoint urls and header values in the Interceptor class in the question are not the real values, I have changed them for the sake of privacy.
Thanks for the help.
It turned out my code was ok and the problem was in the back-end, thanks