package com.reactnativetink.api import okhttp3.Call import okhttp3.Callback import okhttp3.FormBody import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import okhttp3.logging.HttpLoggingInterceptor import org.json.JSONObject import java.io.IOException class TinkAuthorizationCode { fun getAuthorizationCode( appAccessToken: String, userId: String, idHint: String, actorClientId: String, scope: String, credentialId: String, onGetAuthorizationCodeListener: OnGetAuthorizationCodeListener, ) { val formBody = FormBody.Builder() .addEncoded("user_id", userId) .addEncoded("id_hint", idHint) .addEncoded("actor_client_id", actorClientId) .addEncoded("scope", scope) .build() val logging = HttpLoggingInterceptor() logging.level = HttpLoggingInterceptor.Level.BODY val okhttpClient = OkHttpClient.Builder().addInterceptor(logging).build() val request = Request .Builder() .url("https://api.tink.com/api/v1/oauth/authorization-grant/delegate") .addHeader("Authorization", "Bearer $appAccessToken") .addHeader("Content-Type", "application/x-www-form-urlencoded") .post(formBody) .build() okhttpClient.newCall(request).enqueue( object : Callback { override fun onFailure( call: Call, e: IOException, ) { e.printStackTrace() onGetAuthorizationCodeListener.onGetAuthorizationCodeFailure() } override fun onResponse( call: Call, response: Response, ) { if (response.isSuccessful) { val authorizationCode = JSONObject(response.body?.string()).getString("code") onGetAuthorizationCodeListener.onGetAuthorizationCodeSuccess(authorizationCode, credentialId) } } }, ) } }