Android注释登录失败401未经授权

时间:2015-12-28 04:24:02

标签: java android restful-authentication http-status-code-401 android-annotations

我正在使用Android Annotations来管理我的客户端 - 网络服务通信。

当我尝试使用AA登录到端点时,我收到401 Unauthorized错误,但是当我使用Postman时,我可以成功登录到该端点并获得响应。

My Rest Client代码如下:

@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);
}

我的登录模型是这样的:

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;
}
}

我使用的网址是这样的:

  public static final String BASE_URL = "https://api.com/api/";


  public static final String LOGIN = "login";

我正在使用的Interceptor类是这样的:

@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);

}
}

正如我之前所说,当我在Postman上使用这些值时,我得到了一个成功的响应,但是当我在应用程序中尝试这个时,我得到了401错误。

我尝试了一些解决方案并试图在拦截器中添加BasicHttpAuth但是没有用,我想知道这是一个常见问题还是API存在问题。

注意:问题中Interceptor类中的API端点url和header值不是真正的值,为了保护隐私,我更改了它们。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

原来我的代码还可以,问题出在后端,谢谢

相关问题