首页 > 解决方案 > 如何解决android中的Sinch Invalid signature错误

问题描述

我正在尝试使用 sinch 的 callout rest api 加入电话会议,但在使用改造时获得响应 40102 无效签名。

我的代码是..

        val instant: Instant = Instant.now()

        val mainObject = JsonObject()
        mainObject.addProperty("method", "conferenceCallout")
        val jsonConferenc = JsonObject()
        val jsonDestination = JsonObject()
        jsonDestination.addProperty("type", "username")
        jsonDestination.addProperty("endpoint", getUserInfo().id)
        jsonConferenc.add("destination", jsonDestination)

        jsonConferenc.addProperty("domain", "mxp")
        jsonConferenc.addProperty("conferenceId", "4495")
        jsonConferenc.addProperty("enableDice", false)
        mainObject.add("conferenceCallout", jsonConferenc)

        val contentMd5 = Base64.encodeToString(getMd5(mainObject.toString())?.toByteArray(), Base64.NO_WRAP)
        val stringToSign = "POST\n" +
                "$contentMd5\n" +
                "application/json; charset=UTF-8\n" +
                "x-timestamp:$instant\n" +
                "/v1/callouts"

        val key = Base64.decode(SinchServiceNew.APP_SECRET, Base64.DEFAULT)
        val secret_key = SecretKeySpec(SinchServiceNew.APP_SECRET.toByteArray(Charsets.UTF_8), "HmacSHA256")
        val sha256_HMAC = Mac.getInstance("HmacSHA256")
        sha256_HMAC.init(secret_key)
        val signature = Base64.encodeToString(sha256_HMAC.doFinal(stringToSign.toByteArray(Charsets.UTF_8)), Base64.NO_WRAP)

        val headers = HashMap<String, String>()
        headers["content-type"] = "application/json; charset=UTF-8"
        headers["x-timestamp"] = instant.toString()
        headers["authorization"] = "Application ${SinchServiceNew.APP_KEY}:$signature"


标签: androidrestkotlinsinchconference

解决方案


推荐阅读