1

I'm making an Android application with koltin that connects to AWS- Amplify

My problem is: When creating a REST request, I am getting the 401 error, that is, I am not authorized to make this request. I've already used amplify's Auth.signIn function and had success with it. Below you will see 2 ways that I tried: using an interceptor so that every request is added a header with the authorization token and an attempt mocking the token directly in the function. Both returned the same error.

REST Request

fun onboardUser() {
        try {
            // val authToken = "anyToken"

            val options = RestOptions.builder()
                .addPath("/api/v1/me/onboard")
                // .addHeaders(mapOf("Authorization" to authToken))
                .build()

            API.put(
                options,
                { Log.i("AmplifyOnboardSuccess", "PUT succeeded: $it") },
                { Log.e("AmplifyOnboardError", "PUT failed", it) }
            )
        } catch (e: Exception) {
            // tratar exceção
        }
    }

Interceptor

suspend fun amplifyInterceptor() = suspendCoroutine<Unit> { continuation ->
        try {
            val authProviders = ApiAuthProviders.builder()
                .oidcAuthProvider {
                    val session = runBlocking {
                        com.amplifyframework.kotlin.core.Amplify.Auth.fetchAuthSession()
                    }
                    (session as AWSCognitoAuthSession).userPoolTokensResult.value?.idToken ?: ""
                }
                .build()
            val plugin = AWSApiPlugin.builder()
                .configureClient(REST_API) { okHttpBuilder ->
                    okHttpBuilder.addInterceptor { chain ->
                        val token = authProviders.oidcAuthProvider.latestAuthToken
                        val originalRequest = chain.request()
                        val updatedRequest = originalRequest.newBuilder()
                            .addHeader("Authorization", token)
                            .build()
                        chain.proceed(updatedRequest)
                    }
                }
                .build()
            com.amplifyframework.kotlin.core.Amplify.addPlugin(plugin)
            Log.i("AmplifyKeysSuccess", "Succeeded to fetch session")
            continuation.resume(Unit)
        } catch (e: GeneralException) {
            e.printStackTrace()
            Log.e("AmplifyKeysError", "Failed to fetch session", e)
            continuation.resumeWithException(e)
        }
    }

Logcat

2023-07-20 17:41:31.307 10144-11103 AmplifyOnboardSuccess   com.fortknoxster.debug               I  PUT succeeded: RestResponse{data=Data{rawBytes=[123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 85, 110, 97, 117, 116, 104, 111, 114, 105, 122, 101, 100, 34, 125]}, code=Code{statusCode=401}}

configure()

fun configureBridge() {
        GlobalScope.launch(Dispatchers.IO) {
            amplifyInterceptor()
            configure()
        }
    }

suspend fun configure() {
        try {
            com.amplifyframework.kotlin.core.Amplify.addPlugin(AWSCognitoAuthPlugin())
            com.amplifyframework.kotlin.core.Amplify.addPlugin(AWSDataStorePlugin())
            com.amplifyframework.kotlin.core.Amplify.addPlugin(AWSS3StoragePlugin())
            com.amplifyframework.kotlin.core.Amplify.configure(contextAplication)
        } catch (error: AmplifyException) {
            Log.e("AmplifyStartError", "Could not initialize Amplify", error)
        }
    }

0 Answers0