简体   繁体   中英

How to add multiple authorization in retrofit response in android

enter image description here在此处输入图像描述

I have using the multiple authorization for retrofit response, for example signature method, Consumer key, Consumer secret, Access token, Token secret and Realm. I don't know how to add this. How to add authorization using retrofit in android?

 @Singleton
    @Provides
    @Named("Sms")
    fun provideRetrofitString(httpClientBuilder: OkHttpClient.Builder, retrofit: Retrofit): Retrofit {
    val auth="3667364"+"338906b74233cccf69cce16c0401483303f5491e082cb5e4c947010867ada1da"+"28d62c8e1418303bab22f01766e486914190710cc1513048588a851d4c9fa9f7"+"HMAC-SHA1"+"1672114347"+"8PbVZguVm2J"+"1.0"+"YTdxdhYEOHWNBv1ixbj1yulpeQU%3D"
            httpClientBuilder.also {
                it.hostnameVerifier { _, _ -> true }
                it.addInterceptor { chain ->
                    chain.proceed(chain.request().newBuilder()
                        .addHeader("Content-Type", "text/json")
                        .addHeader("Charset", "utf-8")
                        .addHeader("Authorization", "Bearer $auth")
                        .build())
                }
            }
    
            return retrofit.newBuilder().let {
                it.baseUrl(NetworkConstants.BASE_URL_NS_RESTLET)
                it.addConverterFactory(GsonConverterFactory.create())
                it.client(httpClientBuilder.build())
                it.build()
            }
}

Authorization is basically a "Header" so anything you want to add as an authorization to your REST API Request you can add it using the function addHeader(name: String, value: String) which you are already using to add "Content-Type".

So for example, if you want to add a Bearer Token then your code will be like this:

@Singleton
@Provides
@Named("Sms")    
fun provideRetrofitString(httpClientBuilder: OkHttpClient.Builder, retrofit: Retrofit): Retrofit {
httpClientBuilder.also {
    it.hostnameVerifier { _, _ -> true }
    it.addInterceptor { chain ->
        chain.proceed(chain.request().newBuilder()
                .addHeader("Content-Type", "text/json")
                .addHeader("Charset", "utf-8")
                .addHeader("Authorization", "Bearer $YOUR_TOKEN")
                .build())
    }
}

As an additional tip, create your interceptor as an independent class that handles all your requests' interception logic and then attach it to your HTTPClient. Your Interceptor class would implement the Interceptor interface from OKHTTP and in there you have to override the "intercept" method which will provide you with the chain which in turn you can use to access the request. An example would look like this:

class WebServiceInterceptor(private val token: String) : Interceptor {

  override fun intercept(chain: Interceptor.Chain): Response {
    val request = chain.request()
    val requestBuilder = request.newBuilder()

    // adding essential headers
    requestBuilder.addHeader("Content-Type", "text/json")
    requestBuilder.addHeader("Charset", "utf-8")
    requestBuilder.addHeader("Authorization", "Bearer $token")

    return chain.proceed(requestBuilder.build())
  }
}

then attach it to the HTTPClient like this:

@Singleton
@Provides
@Named("Sms")    
fun provideRetrofitString(httpClientBuilder: OkHttpClient.Builder, retrofit: Retrofit, webServiceInterceptor: WebServiceInterceptor): Retrofit {
httpClientBuilder.also {
    it.hostnameVerifier { _, _ -> true }
    it.addInterceptor(webServiceInterceptor)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM