【问题标题】:403 Forbidden. The request signature we calculated does not match signature you provided. Check your key and signing method from Android to amazon s3403 禁止。我们计算的请求签名与您提供的签名不匹配。检查从 Android 到 amazon s3 的密钥和签名方法
【发布时间】:2022-06-30 01:58:48
【问题描述】:

我正在尝试使用 aws 签名方法调用 get api,但无法获得响应。 下面是我的代码。

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())

无法弄清楚,我在做什么错误。 请帮我解决这个问题。

【问题讨论】:

    标签: android amazon-web-services kotlin amazon-s3


    【解决方案1】:

    如果您想创建一个用 Kotlin 编写的 Android 应用程序并调用 AWS 服务,请使用 AWS SDK for Kotlin。该服务有一个强类型的服务客户端,您可以在 Android Studio 项目中使用它来调用给定的服务。

    例如,这里是使用强类型服务客户端调用 SNS 的 Kotlin 代码。

        // 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
        }
    

    要了解如何使用适用于 Kotlin 的 AWS 开发工具包,请参阅

    AWS SDK for Kotlin Developer Guide

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2020-11-06
      • 2019-12-01
      • 2015-12-31
      • 2021-04-11
      • 1970-01-01
      • 2015-06-01
      • 2015-05-18
      相关资源
      最近更新 更多