安卓注释登录失败401未经授权



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

当我尝试使用AA登录到一个端点时,我会收到401未经授权的错误,但我可以成功登录到该端点,并在使用Postman时得到响应。

我的休息客户端代码如下:

@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和头值不是真正的值,为了隐私起见,我更改了它们。

谢谢你的帮助。

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

最新更新