问题
I'm trying to call a restful API using retrofit(retrofit:2.3.0) in Android and the API calls work from the Postman and iOS but do not work for the Android. Apis working fine which does not use JWT Authorization header(Bearer auth_token) like login & signup API but when I try with Authorization header it returns 403 forbidden. I'm getting cookies header too on login and sign-up response.
Here is my code;
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
final Call<GetUserResponse> response = apiService.getUserInformation(ConstantUtils.TOKEN_PREFIX + sharedPreferences.getString(ConstantUtils.PREF_TOKEN, ""));
response.enqueue(new Callback<GetUserResponse>() {
@Override
public void onResponse(Call<GetUserResponse> call, retrofit2.Response<GetUserResponse> rawResponse) {
try {
if (rawResponse.code() == 200 && rawResponse.body() != null) {
saveUserModelInSharedPreferences(rawResponse.body());
stopSelf();
} else if (rawResponse.code() == 401) {
// we will have to call login api here
CallLoginApiIfFails callLoginApiIfFails = new CallLoginApiIfFails(GetUserDataService.this, GET_USER_DATA);
callLoginApiIfFails.OnApiFailDueToSessionListener(GetUserDataService.this);
}
} catch (Exception e) {
e.printStackTrace();
stopSelf();
}
}
@Override
public void onFailure(Call<GetUserResponse> call, Throwable throwable) {
stopSelf();
}
});
and this retrofit interface;
@GET("user")
Call<GetUserResponse>
getUserInformation(@Header("Authorization") String token);
Api client is
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(ConstantUtils.BASE_URL)
.client(okClient())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
and the error log is;
Response{protocol=http/1.1,
code=403, message=Forbidden,
url=http://pointters-api-dev3.us-east-1.elasticbeanstalk.com:9000/user/setting}
来源:https://stackoverflow.com/questions/45895647/getting-response-403-forbidden-when-trying-to-call-rest-api-hosted-on-aws-using