title: How to add Authorization header in retrofit call using Dagger 2
body: <p>I have a working code example of Token Based Api Authentication(Laravel Passport), but it's without Dagger 2. I want to move code from current <code>RetrofitBuilder.java</code> to AppModule, so that I can use the power of the Dagger 2 in the app.
  <br>
  <br>
  For Now, Without Dagger Dependency, I have two static methods in <code>RetrofitBuilder.java</code> which could be used for authenticated routes calls and unauthenticated routes calls:
  <br>
  <br>
  1) <strong><code>createService(service)</code></strong> // Which makes API call without Authorization Headers (without Access Token)
  <br>
  <br>
  2) <strong><code>createServiceWithAuth(service, tokenManager)</code></strong> // Which makes API call with Authorization Headers e.g with Access Token
  <br></p>
  
  <p><br>
  How to achieve same functionality using Dagger dependency. How can I achieve providing these above two methods to my business logic class e.g <code>UserRepository.java</code> so that I can make authenticated and unauthenticated service calls any where it is necessary.</p>
  
  <p><strong>RetrofitBuilder.java</strong></p>
  
  <pre><code>public class RetrofitBuilder {
  
      private static final String BASE_URL = "https://api.com/";
  
      private final static OkHttpClient client = buildClient();
      private final static Retrofit retrofit = buildRetrofit(client);
  
      private static OkHttpClient buildClient(){
          OkHttpClient.Builder builder = new OkHttpClient.Builder()
                  .addInterceptor(new Interceptor() {
                      @Override
                      public Response intercept(Chain chain) throws IOException {
                          Request request = chain.request();
  
                          Request.Builder builder = request.newBuilder()
                                  .addHeader("Accept", "application/json")
                                  .addHeader("Connection", "close");
  
                          request = builder.build();
  
                          return chain.proceed(request);
  
                      }
                  });
  
          if(BuildConfig.DEBUG){
              builder.addNetworkInterceptor(new StethoInterceptor());
          }
  
          return builder.build();
  
      }
  
      private static Retrofit buildRetrofit(OkHttpClient client){
          return new Retrofit.Builder()
                  .baseUrl(BASE_URL)
                  .client(client)
                  .addConverterFactory(MoshiConverterFactory.create())
                  .build();
      }
  
      public static &lt;T&gt; T createService(Class&lt;T&gt; service){
          return retrofit.create(service);
      }
  
      public static &lt;T&gt; T createServiceWithAuth(Class&lt;T&gt; service, final TokenManager tokenManager){
  
          OkHttpClient newClient = client.newBuilder().addInterceptor(new Interceptor() {
              @Override
              public Response intercept(Chain chain) throws IOException {
  
                  Request request = chain.request();
  
                  Request.Builder builder = request.newBuilder();
  
                  if(tokenManager.getToken().getAccessToken() != null){
                      builder.addHeader("Authorization", "Bearer " + tokenManager.getToken().getAccessToken());
                  }
                  request = builder.build();
                  return chain.proceed(request);
              }
          }).authenticator(CustomAuthenticator.getInstance(tokenManager)).build();
  
          Retrofit newRetrofit = retrofit.newBuilder().client(newClient).build();
          return newRetrofit.create(service);
  
      }
  
      public static Retrofit getRetrofit() {
          return retrofit;
      }
  }
  </code></pre>
  
  <p><strong>CustomAuthenticator.java</strong> (it refresh the token if the access token is expired)</p>
  
  <pre><code>public class CustomAuthenticator implements Authenticator {
  
      private TokenManager tokenManager;
      private static CustomAuthenticator INSTANCE;
  
      private CustomAuthenticator(TokenManager tokenManager){
          this.tokenManager = tokenManager;
      }
  
      static synchronized CustomAuthenticator getInstance(TokenManager tokenManager){
          if(INSTANCE == null){
              INSTANCE = new CustomAuthenticator(tokenManager);
          }
  
          return INSTANCE;
      }
  
  
      @Nullable
      @Override
      public Request authenticate(Route route, Response response) throws IOException {
  
          if(responseCount(response) &gt;= 3){
              return null;
          }
  
          AccessToken token = tokenManager.getToken();
  
          ApiService service = RetrofitBuilder.createService(ApiService.class);
          Call&lt;AccessToken&gt; call = service.refresh(token.getRefreshToken() + "a");
          retrofit2.Response&lt;AccessToken&gt; res = call.execute();
  
          if(res.isSuccessful()){
              AccessToken newToken = res.body();
              tokenManager.saveToken(newToken);
  
              return response.request().newBuilder().header("Authorization", "Bearer " + res.body().getAccessToken()).build();
          }else{
              return null;
          }
      }
  
      private int responseCount(Response response) {
          int result = 1;
          while ((response = response.priorResponse()) != null) {
              result++;
          }
          return result;
      }
  }
  </code></pre>
  
  <p><br><br>
  <strong>AppModule.java</strong></p>
  
  <pre><code>@Module(includes = ViewModelModule.class)
  public class AppModule {
  
      // --- DATABASE INJECTION ---
  
      @Provides
      @Singleton
      MyDatabase provideDatabase(Application application) {
          return Room.databaseBuilder(application,
                  MyDatabase.class, "MyDatabase.db")
                  .build();
      }
  
      @Provides
      @Singleton
      UserDao provideUserDao(MyDatabase database) { return database.userDao(); }
  
      // --- REPOSITORY INJECTION ---
  
      @Provides
      Executor provideExecutor() {
          return Executors.newSingleThreadExecutor();
      }
  
      @Provides
      @Singleton
      UserRepository provideUserRepository(UserWebservice webservice, UserDao userDao, Executor executor) {
          return new UserRepository(webservice, userDao, executor);
      }
  
      // --- NETWORK INJECTION ---
  
      private static String BASE_URL = "https://api.com/";
  
      @Provides
      Gson provideGson() { return new GsonBuilder().create(); }
  
      @Provides
      Retrofit provideRetrofit(Gson gson) {
          Retrofit retrofit = new Retrofit.Builder()
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .baseUrl(BASE_URL)
                  .build();
          return retrofit;
      }
  
      @Provides
      @Singleton
      UserWebservice provideApiWebservice(Retrofit restAdapter) {
          return restAdapter.create(UserWebservice.class);
      }
  }
  </code></pre>