Android retrofit2 get token of oauth2공부방/Android2020. 2. 24. 12:58
Table of Contents
class AuthenticateInterceptor() : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
request.let {
val builder = it.newBuilder()
val credentials = Credentials.basic("client_id", "client_secret")
builder.addHeader("Authorization", credentials)
request = builder.build()
}
return chain.proceed(request)
}
}
Retrofit.Builder()
.client(getOkHttpClientBuilder(AuthenticateInterceptor()).build())
.baseUrl(GSConfig.getRestBaseUrl(true))
.addConverterFactory(GsonConverterFactory.create())
.build().create(CommonRestApi::class.java)
CommonRestApi class
@POST("/oauth/token")
@FormUrlEncoded
fun getAuthToken(): Call<OAuthTokenResponse>
How to use
val call = RetrofitCreator.getInstance().createCommonApi().getAuthToken()
call.enqueue(object : SDCallback<OAuthTokenResponse>(activity) {
override fun onSuccess(response: Response<OAuthTokenResponse>) {
response.body()?.let {
oAuthTokenResponse.postValue(it)
} ?: let {
oAuthTokenResponse.postValue(null)
}
}
override fun onFail(apiError: APIError) {
oAuthTokenResponse.postValue(null)
}
override fun onNetworkError() {
oAuthTokenResponse.postValue(null)
}
})
반응형
'공부방 > Android' 카테고리의 다른 글
@soycrab :: 꿀맛코딩
행복한 코딩을 위하여!
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!