Add a header to a retrofit request without an Interceptor

Viewed 172

I have a retrofit api interface where one of the calls requires OAuth1 authentication and the others do not. I am trying to find a way to add the Authentication header without using an Interceptor for a few reasons.

  1. At the time the OkHttp Client is created I wont know OAuth1 keys and the client is used for other things too.
  2. It is only one request that needs OAuth1 authentication

What I was trying to do is wrap the interface call in a Call and then adding the header before I process

val call = api.getRecentTweetsV1("$query -filter:replies -filter:retweets")

val oauth = Oauth1Signing.Builder()
    .consumerKey(consumerKey)
    .consumerSecret(consumerSecret)
    .accessToken(token)
    .accessSecret(tokenSecret).build()

val authHeader = oauth.signRequest(call.request())
call.request().headers.newBuilder().add("Authorization", authHeader).build()
val headers = call.request().headers

response = call.execute().body()

The problem is I don't see the header getting added in, the call.request().headers value is always empty after I try to add it in.

I cant use the @Header parameter in the interface call because the OAuth1Signer (modified version of this, I removed the Interceptor usage) takes in a OkHttp Request object to build the header.

Is there another way to add the header after the request has been built?

0 Answers
Related