Getting response 403 forbidden when trying to call rest api hosted on AWS using node.js

筅森魡賤 提交于 2019-12-11 02:48:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!