Skip to main content

How to perform authentication using OKHTTP in Kotlin Android

How to perform authentication using OKHTTP in Kotlin Android.

In this tutorial, we will learn how to perform authentication using OkHttp library in a Kotlin Android application. OkHttp is a widely used library for making HTTP requests in Android applications. It provides a simple and efficient API for making network calls.


To follow along with this tutorial, you should have a basic understanding of Kotlin and Android development. You should also have Android Studio installed on your machine.

Step 1: Add OkHttp Dependency

First, we need to add the OkHttp dependency to our project. Open your project's build.gradle file and add the following line to the dependencies block:

implementation 'com.squareup.okhttp3:okhttp:4.9.1'

Sync your project after adding the dependency.

Step 2: Create an Authentication Service

Next, let's create a class to handle the authentication process. Create a new Kotlin file called AuthenticationService.kt and add the following code:

import okhttp3.Authenticator
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route

class AuthenticationService : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
// Implement your authentication logic here

// Example: Add authentication header to the request
val authenticatedRequest = response.request.newBuilder()
.header("Authorization", "Bearer your_token_here")

return authenticatedRequest

In this code, we are creating a class AuthenticationService that implements the Authenticator interface provided by OkHttp. The authenticate method is called when the server responds with a 401 Unauthorized status code. Inside this method, you can implement your own authentication logic. In this example, we are simply adding an authentication header to the request.

Step 3: Create an OkHttp Client with Authenticator

Now, let's create an OkHttp client with our AuthenticationService as the authenticator. Open your desired Kotlin file and add the following code:

import okhttp3.OkHttpClient

val client = OkHttpClient.Builder()

Here, we are creating an OkHttpClient instance using the OkHttpClient.Builder class. We are setting our AuthenticationService as the authenticator for this client using the authenticator method.

Step 4: Make Authenticated Requests

With our OkHttpClient configured with the authentication service, we can now make authenticated requests. Here's an example of how to make a GET request:

import okhttp3.Request

val request = Request.Builder()

val response = client.newCall(request).execute()
val responseBody = response.body?.string()

In this code, we are creating a Request object with the desired URL. We then use the OkHttpClient instance to execute the request using the newCall method. Finally, we can access the response body using the response.body?.string() method.


In this tutorial, we learned how to perform authentication using OkHttp in a Kotlin Android application. We created an AuthenticationService class to handle the authentication logic and configured an OkHttpClient with this service as the authenticator. We then made authenticated requests using the OkHttpClient. OkHttp provides a powerful and flexible API for handling authentication in Android applications.