如何使用Kotlin在带有Volley的Android中使用Hmac SHA1获得oauth_signature?

时间:2019-06-26 12:58:03

标签: android kotlin oauth android-volley hmacsha1

我正在尝试使用科特林中的 Volley GET 方法从 woo商业api 获得响应。我已经生成了所有必需的参数,但是无法获得响应。

GET网址的格式必须是这样

http://www.tronosoft.in/demo/wordpress/wp-json/wc/v2/products?oauth_consumer_key=ck_22694c9d56cc1e03cff4cc2615xxxxxxxxxxx&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1561550709&oauth_nonce=cd22f6f0-d37e-46b9-9ae2-b49e4c33d6aa&oauth_version=1.0&oauth_signature=W0JAOThkMGZkNQ==

我用来生成所有参数的代码是

fun productApi() {
    //val oauth = AuthPermission() //Auth0(String(""))
    val consumerKey = "ck_22694c9d56cc1e03cff4cc2615xxxxxxxxxxxx"
    val consumerSecret = "cs_57ed10e0cb3e2de073184983ffxxxxxxxxxxxx"
    val nonce = UUID.randomUUID()
    val time: Long = System.currentTimeMillis() / 1000
    Log.d("PRODUCTTIME", time.toString())
    val url = baseUrl + "products?oauth_consumer_key=ck_22694c9d56cc1e03cff4cc2615xxxxxxxxxxxx&oauth_signature_method=HMAC-SHA1&oauth_timestamp=$time&oauth_nonce=mKcxEHLuAVk&oauth_version=1.0"

    val signature = Mac.getInstance("HmacSHA1")
    val secret = SecretKeySpec(consumerKey.toByteArray(), signature.algorithm)
    signature.init(secret)


    Log.d("PRODUCTNAL", signature.toString())

    val digssss = signature.doFinal(url.toByteArray())

    Log.d("PRODUCTDIG", digssss.toString())

    val result = Base64.encodeUrlSafe(digssss.toString())

    Log.d("PRODUCTSIG", result.toString())

    val urlT = baseUrl + "products?oauth_consumer_key=" + consumerKey + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=$time&oauth_nonce=$nonce&oauth_version=1.0&oauth_signature=$result"

    Log.d("PRODUCTURL", urlT)

    val request = object : StringRequest(Method.GET, urlT, Response.Listener { response ->
        Log.d("PRODUCTLIST", response.toString())
    }, Response.ErrorListener { error ->
        Log.d("PRODUCTERROR", error.toString())
    }){}
    val requestQ = Volley.newRequestQueue(context)
    requestQ.add(request)
}

但是我收到错误 com.android.volley.AuthFailureError 并且在网络上显示无效签名-提供的签名不匹配

我在SO上查找了有关此问题的其他问题,但是与Volt和Kotlin无关。

0 个答案:

没有答案