Retrofit Bearer token

retrofit token authentication
retrofit 2'',authorization header
how to send token in header in android
okhttp bearer token
retrofit add token to header
retrofit authenticator
logging in retrofit
android oauth2 retrofit

I try to receive token via POST json {"email":"","password":"test"}. In postman it works: Postman request. I try do the same in Android Studio. I create class Token:

   public class Token {
    public String token;

And class APIclient

    class APIClient {

    private static Retrofit retrofit = null;

    static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        retrofit = new Retrofit.Builder()

        return retrofit;


and interface APIInterface:

    interface APIInterface {
    Call<Token> getLoginResponse(@Body AuthenticationRequest request);

and class AuthenticationRequest:

    public class AuthenticationRequest {
    String email;
    String password;

In onCreate in MainActivity:

  apiInterface = APIClient.getClient().create(APIInterface.class);"";

And here is my getTokenResponse method:

    private void getTokenResponse() {
    Call<Token> call2 = apiInterface.getLoginResponse(authenticationRequest);
    call2.enqueue(new Callback<Token>() {
        public void onResponse(Call<Token> call, Response<Token> response) {
            Token token = response.body();
        public void onFailure(Call<Token> call, Throwable t) {

And this is what I see in Logcat:

    03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: --> POST http/1.1
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: Content-Type: application/json; charset=UTF-8
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: Content-Length: 46
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: {"email":"","password":"test"}
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: --> END POST (46-byte body)

Could you tell me what I'm doing wrong? I need to give token every time when I'd like to get information from server via GET method. How can I receive and save token in Android code?

you should add application/json to header

interface APIInterface {
@Headers({"Content-Type: application/json", "Accept: application/json"})
Call<Token> getLoginResponse(@Body AuthenticationRequest request);

How to pass Authorization Bearer using retrofit?, I am currently using retrofit within an android app, and okhttp client. When signing my request headers with the auth token using the required  Get Results Now! Search For Bearer Token. Save Time using

Try this

 interface APIInterface {
  Call<Token> getLoginResponse(@Header("Authorization") String token, @Body AuthenticationRequest request);

token is the Bearer token

Bearer Token Header with OKHttp, Retrofit is a type-safe HTTP client by Square that was built for the Android platform. It offers an easy and clean way to make REST API network calls and parses  Retrofit (v2.5.0) and OkHttp (v3.14.0) are open source rest client libraries for Android. You can use these libraries in your project to easily consume your web APIs. In this tutorial we are going to learn how to implement OAuth2(bearer Token) authentication in your app using the retrofit.

To get access token from you need to get header from response and read values from headers.

Callback<User> user = new Callback<User>() {
    public void success(User user, Response response) {
        List<Header> headerList = response.getHeaders();
        //iterating list of header and printing key/value. 
        for(Header header : headerList) {
            Log.d(TAG, header.getName() + " " + header.getValue());

After you get the value you need from header i.e. AccessToken, you can store that value in Storage. e.g. SharedPreference

So next time when you need that value you can directly get that from Storage. e.g. SharedPreference

Now when you pass request for any webservice either that is GET or POST or any other method. You have to pass that into header requests.

Call<List<Task>> getTasks(@Header("Content-Range") String contentRange);

Or If you need to pass it every time, you can directly pass that from your Retrofit class.

Request request = original.newBuilder()
        .header("User-Agent", "Your-App-Name")
        .header("Accept", "application/vnd.yourapi.v1.full+json")
        .method(original.method(), original.body())

Token-Based Authentication with Retrofit, Retrofit, most popular networking library in the recent times. The convenient Use Case 1: Some API returns the updated token in response header for once. Display text programmatically for TextView. I am looking to set a text programmatically, but for some reason, when I am doing so, no text appears on the screenI have attached my code below

Advanced Retrofit2 (Part 2): Authorization Handling, Add customized headers via Retrofits 2.0 in our Android app. There are many tutorials about using interceptor to add headers in Retrofit. You can add  We’ll cover the topic of token authentication from an Android app to any web service or API supporting this kind of authentication. Update — October 22nd 2015. We’ve added new code examples for Retrofit 2 besides the existing ones for Retrofit 1.9. We’ve also published an extensive Retrofit upgrade guide. Retrofit Series Overview

Retrofit Interceptor Add Authorization Token - Jaimin Patel, The app uses Retrofit with OkHttp under the hood, and it looks like the AUTH, "​Token refresh failed during process of: %s", what); return;  Token based authentication using Retrofit 1.9 + OkHttp 2.4 27 Jul 2015. Situation is like this: You got an AccessToken and RefreshToken (AT and RT for now on) Every API call needs to contain the AT ; When the AT expires you need to refresh it using your RT; Interceptor: Attaching Access Token to every network request. What you need is a Network

Retrofit Tutorial, Contribute to muzafakar/Retrofit-Bearer-Token development by creating an account on GitHub. Credentials are exchanged against a token which is then attached to every subsequent request. Token are temporary and get expired/refreshed after a certain period of time. Apps might need to get

  • are you getting/sending token from header ?
  • in GET method I send token in header Authorization
  • I have posted answer. Please look at it.