juherr
juherr

Reputation: 5740

How to make HTTP request from Okhttp Authenticator?

I'm using Okhttp3 and I want to build an OAuth2 Authenticator.

Sometimes, I need to make http requests from the Authenticator itself (ie: to refresh the token) but the api doesn't provide a way to do it.

For sure, I can create a new okhttp instance but I don't know if it is a recommanded practice.

Is it a best practice for my need?

Upvotes: 1

Views: 503

Answers (1)

juherr
juherr

Reputation: 5740

It is not possible to do it out of the box but some workarounds can work:

  • create a new instance of OkHttpClient into the Authenticator, or
  • add a setHttpClient method in Authenticator

.

MyAuthenticator authenticator = new MyAuthenticator();
OkHttpClient client = new OkHttpClient.Builder()
    .authenticator(authenticator)
    .build();
authenticator.setHttpClient(client);

From: https://github.com/square/okhttp/issues/2733

Upvotes: 2

Related Questions