403 Forbidden. The request signature we calculated does not match signature you provided. Check your key and signing method from Android to Amazon S3

Viewed 159

I am trying to call get api using an AWS signing method but not able to get the response. Below is my code.

val secretkey = "E+t5/nDf6/NKNJBjbsdjv"
val accesskey = "DJKSBDKSBNKFGNBFG"
val credentials: AWSCredentials = BasicAWSCredentials(accesskey, secretkey)
val API_GATEWAY_SERVICE_NAME = "s3"
val requestAws: Request<*> = DefaultRequest<Any?>(API_GATEWAY_SERVICE_NAME)

val uri = URI.create("https://s3.us-west-2.amazonaws.com/..../../sample")
requestAws.endpoint = uri

requestAws.resourcePath = "https://s3.us-west-2.amazonaws.com/..../../sample"
requestAws.httpMethod = HttpMethodName.GET

val signer = AWS4Signer() signer . setServiceName (API_GATEWAY_SERVICE_NAME)
signer.setRegionName("us-west-2")
signer.sign(requestAws, credentials)
val headers = requestAws.headers

val key: MutableList<String> = ArrayList()
val value: MutableList<String> = ArrayList()

for ((key1, value1) in headers)
{
    key.add(key1) value . add (value1)
}

val httpClient = OkHttpClient()
val request: okhttp3.Request = okhttp3.Request.Builder()
        .url("https://s3.us-west-2.amazonaws.com/..../../sample")
        .addHeader(key[0], value[0])
        .addHeader(key[1], value[1])
        .addHeader(key[2], value[2])
        .addHeader("X-Amz-Content-Sha256", 
         "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
        .build()

val response: okhttp3.Response = httpClient.newCall(request).execute()
Log.i("LOG", response.body.toString())

Not able to figure out, what I am doing mistake. Please help me out with this issue.

1 Answers

If you want to create an Android app written in Kotlin and invokes AWS Services, use the AWS SDK for Kotlin.

This SDK has strongly typed Service Clients that you can use in an Android Studio project that lets you invoke a given service. (as opposed to using okhttp3.Request, etc)

For example, here is Kotlin code that invoke SNS using a Strongly typed service client named SnsClient.

    // Get all subscriptions.
     fun getSubs(view: View) = runBlocking {

        val subList = mutableListOf<String>()
        val snsClient: SnsClient = getClient()
        try {

            val request = ListSubscriptionsByTopicRequest {
                topicArn = topicArnVal
            }
            val response = snsClient.listSubscriptionsByTopic(request)
            response.subscriptions?.forEach { sub ->
                subList.add(sub.endpoint.toString())
            }

            val listString = java.lang.String.join(", ", subList)
            showToast(listString)

       } catch (e: SnsException) {
            println(e.message)
            snsClient.close()
        }
       }

 fun getClient() : SnsClient{

        val staticCredentials = StaticCredentialsProvider {
            accessKeyId = "<Enter key>"
            secretAccessKey = "<Enter key>"
        }

        val snsClient = SnsClient{
            region = "us-west-2"
            credentialsProvider = staticCredentials
        }

        return snsClient
    }

TO learn how to use the AWS SDK for Kotlin, see

AWS SDK for Kotlin Developer Guide

Related