AndroidAnnotations how to use setBearerAuth

496 views Asked by At

I'm trying to authenticate with a REST API via a token using AndroidAnnotations @Rest RestClient Interface.

But due to a lack of documentation I can't get it running using setBearerAuth(String token);

I already have the interface and the class HeadersRequestInterceptor implements ClientHttpRequestInterceptor, but I don't know where to call setBearerAuth(myCustomToken);

If someone could give me a hint I would be very graceful.

Cheers

2

There are 2 answers

0
WonderCsabo On

MyRestClient.java:

@RequiresAuthentication
@Rest(rootUrl = "your_url", converters = {...})
public interface MyRestClient extends RestClientHeaders {

  @Post("/somecall")
  public void someApiCall();
}

MyActivity.java:

@EActivity
public class MyActivity extends Activity {

  @RestService
  MyRestClient client;

  @Background
  void callSomeApi() {
    String accessToken = ... ; // was previously returned from server
    client.setBearerAuth(accessToken);
    client.someApiCall(); // it will add header: Authorization: Bearer accessToken
  }
}
0
Rafael Rocha On

I found out a better way, let-me explain: If you need to apply that in all your Android REST calling, so we need to create a Interceptor to get all REST request made by the application like this:

import android.content.Context;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.rest.spring.annotations.Rest;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;



/**
 * Created by Rafael Rocha on 21/05/2018.
 */
@EBean
public class HttpBasicAuthenticatorInterceptor  implements ClientHttpRequestInterceptor {

    @RootContext
    Context context;

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        request.getHeaders().add("Authorization",AUTHENTICATION_SCHEME + "your_token_goes_here");

        return execution.execute(request, body);
    }
}

Replace AUTHENTICATION_SCHEME by your authentication method, in my case was "Bearer" and don't forget to put a blank space between them "Bearer YOUR_TOKEN". After that is just put a reference in your REST service class on @Rest annotation filling the property "interceptors"

   import org.androidannotations.rest.spring.annotations.Accept;
import org.androidannotations.rest.spring.annotations.Get;
import org.androidannotations.rest.spring.annotations.Header;
import org.androidannotations.rest.spring.annotations.Path;
import org.androidannotations.rest.spring.annotations.Rest;
import org.androidannotations.rest.spring.api.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.util.List;



/**
 * Created by Rafael Rocha on 13/03/2018.
 */

@Rest(rootUrl = RestUtil.ROOT_URL, converters = {MappingJackson2HttpMessageConverter.class}, interceptors = {HttpBasicAuthenticatorInterceptor.class})
@Accept(MediaType.APPLICATION_JSON)
public interface NotificationRest {

    //@Header(name = "server_version", value = SERVER_VERSION)
    @Get("notificacao/profissional/recentes/{idProfessional}")
    List<ResponseNotification> getNotifications(@Path Long idProfessional);
}

The only thing I did was put a Header value in the request header. You can do it to pass other things for example here in my application a need to pass my application android version to server side treats the old application or simply block the access, so I just need to increase on more line in the code

 request.getHeaders().add(RestUtil.SERVER_VERSION_HEADER,RestUtil.SERVER_VERSION);