Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit add header with token and id

I have a problem with getting authenticated user. Before it I got token and user id. Now i need to get user from server using access token and id. I have header format

Now I'am trying to add header with user token and id using interceptor.

My code:

Interceptor interceptor = new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request newRequest = chain.request().newBuilder()
                    .addHeader("Accept", "application/json")
                    .addHeader("authorization", token) <-??
                    .addHeader("driver_id", id) <-??
                    .build();
            return chain.proceed(newRequest);
        }
    };
    OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
    okHttpBuilder.addInterceptor(interceptor);
    OkHttpClient okHttpClient = okHttpBuilder.build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();

Interface:

@GET("driver/v1/driver")
Call<Driver> getAuthorizedDriver();

Different variants throws 401 error, don't know what to do Log:

I/Response code: 401
I/Response message: Unauthorized`
like image 385
Ruslan Myhal Avatar asked Jan 06 '17 21:01

Ruslan Myhal


People also ask

Should I put token in header?

In JWT token authentication, the server-provided token should always be sent as a header with the Authorization: Bearer <token> format. The website then should check the validity of the token when a request comes and handle it accordingly. Yes, the use of HTTPS is mandatory.

Why tokens are passed in header?

This allows attackers to obtain sensitive data such as usernames, passwords, tokens (authX), database details, and any other potentially sensitive data.

How do I send a POST request with Basic Auth in retrofit?

You will have to create a Request interceptor ( BasicAuthInterceptor ) which extends Interceptor class of OkHttp library. Then, override intercept function and add your credentials into the request. Generate basic credentials with Credentials class of package OkHttp by using its basic function.


2 Answers

I got it. It's must look like:

@GET("driver/v1/driver")
Call<Driver> getAuthorizedDriver(@Header("authorization") String auth);

And auth:

Call<Driver> call = apiInterface.getAuthorizedDriver("Token token=" + token + ", driver_id=" + id);
like image 181
Ruslan Myhal Avatar answered Oct 09 '22 22:10

Ruslan Myhal


Try to pass the header values via the method call:

@GET("driver/v1/driver")
Call<Driver> getAuthorizedDriver(@Header("authorization") String token,
                                 @Header("driver_id") Integer id);

You also wouldn't have to deal with this huge chunk of Interceptor code

like image 44
Malik Avatar answered Oct 09 '22 23:10

Malik