Authorization via Java

Axandar9 years ago

Hi,

I tried to get data from /api/async, but after log in and request to server I get code 400

class talkToWebSite extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... strings) {
            OkHttpClient client = new OkHttpClient();
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            client.interceptors().add(interceptor);

            Retrofit builder1 = new Retrofit.Builder()
                    .baseUrl("http://ip:8082/")
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            TraccarApi traccarApi = builder1.create(TraccarApi.class);
            Call<Login> call1 = traccarApi.postUser("admin", "admin");
            Response responseLogin = null;
            try {
                responseLogin = call1.execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Headers headers =  responseLogin.headers();
            Data data = (Data) getDataWhenLogged(headers, client).body();
            return data.getDataset().getData_latitude() + "";
        }

        protected void onPostExecute(String result) {
            longitiude.setText(result);
        }
    }

    private Response getDataWhenLogged(Headers headers, OkHttpClient client2){
        final String cookieFromHeader = headers.get("Set-Cookie");
        //Log.d("APPLICATION", cookieFromHeader);
        final Headers headersFinal = headers;
        OkHttpClient client = new OkHttpClient();
        client.networkInterceptors().add(new Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();

                Request request = original.newBuilder().headers(headersFinal)
                        .addHeader("Set-Cookie", cookieFromHeader)
                        .method(original.method(), original.body())
                        .build();
                return chain.proceed(request);
            }
        });
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client.interceptors().add(interceptor);

        Retrofit builder1 = new Retrofit.Builder()
                .baseUrl("http://ip:8082/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        TraccarApi traccarApi = builder1.create(TraccarApi.class);
        Call<Data> call1 = traccarApi.getData();
        Response response = null;
        try {
            response = call1.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Headers hd = response.headers();
        //Log.d("APPLICATION", hd.get("Set-Cookie"));
        return response;
    }

My logs:

11-29 13:34:04.082 18211-19336/com.axandar.geoviewapp D/OkHttp: --> GET /api/login?email=admin&password=admin HTTP/1.1
11-29 13:34:04.082 18211-19336/com.axandar.geoviewapp D/OkHttp: --> END GET
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: <-- HTTP/1.1 200 OK (58ms)
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: Date: Sun, 29 Nov 2015 13:34:03 GMT
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: Content-Type: application/json; charset=UTF-8
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: Access-Control-Allow-Origin: *
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: Access-Control-Allow-Methods: GET, POST
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: Set-Cookie: JSESSIONID=jfty4fe8rxfi17dyuac64iqlg;Path=/api
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: Expires: Thu, 01 Jan 1970 00:00:00 GMT
11-29 13:34:04.140 18211-19336/com.axandar.geoviewapp D/OkHttp: Content-Length: 189
11-29 13:34:04.141 18211-19336/com.axandar.geoviewapp D/OkHttp: Server: Jetty(9.2.14.v20151106)
11-29 13:34:04.141 18211-19336/com.axandar.geoviewapp D/OkHttp: OkHttp-Selected-Protocol: http/1.1
11-29 13:34:04.141 18211-19336/com.axandar.geoviewapp D/OkHttp: OkHttp-Sent-Millis: 1448804044097
11-29 13:34:04.141 18211-19336/com.axandar.geoviewapp D/OkHttp: OkHttp-Received-Millis: 1448804044139
11-29 13:34:04.141 18211-19336/com.axandar.geoviewapp D/OkHttp: {"success":true,"data":{"name":"admin","language":"","id":1,"map":"","readonly":false,"distanceUnit":"","speedUnit":"","latitude":0.0,"longitude":0.0,"admin":true,"email":"admin","zoom":0}}
11-29 13:34:04.141 18211-19336/com.axandar.geoviewapp D/OkHttp: <-- END HTTP (189-byte body)
11-29 13:34:04.144 18211-19336/com.axandar.geoviewapp D/OkHttp: --> GET /api/async/ HTTP/1.1
11-29 13:34:04.144 18211-19336/com.axandar.geoviewapp D/OkHttp: --> END GET
11-29 13:34:04.152 18211-19336/com.axandar.geoviewapp D/OkHttp: <-- HTTP/1.1 400 No Host (7ms)
11-29 13:34:04.152 18211-19336/com.axandar.geoviewapp D/OkHttp: Content-Length: 0
11-29 13:34:04.152 18211-19336/com.axandar.geoviewapp D/OkHttp: Connection: close
11-29 13:34:04.152 18211-19336/com.axandar.geoviewapp D/OkHttp: Server: Jetty(9.2.14.v20151106)
11-29 13:34:04.152 18211-19336/com.axandar.geoviewapp D/OkHttp: OkHttp-Selected-Protocol: http/1.1
11-29 13:34:04.152 18211-19336/com.axandar.geoviewapp D/OkHttp: OkHttp-Sent-Millis: 1448804044145
11-29 13:34:04.152 18211-19336/com.axandar.geoviewapp D/OkHttp: OkHttp-Received-Millis: 1448804044150
11-29 13:34:04.152 18211-19336/com.axandar.geoviewapp D/OkHttp: <-- END HTTP (0-byte body)

Is there another way to authorize second call?

Anton Tananaev9 years ago

I don't think it's authorization issue. It seems like something is wrong with your request.

As for authorization, you can either use cookies or basic HTTP authorization.

Axandar9 years ago

Thank you for answer.
Basic authentication solved problem.

String credentials = "admin:admin";
        final String basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
        client.interceptors().add(new Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();
                Request.Builder requestBuilder = original.newBuilder()
                    .header("Authorization", basic)
                    .method(original.method(), original.body());
                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });