qiniu/java-sdk

直播云API接口报{"error":"unauthorized"}错误

Opened this issue · 0 comments

mdddj commented

这是我的签名函数

open class QiNBaseController {


    @Value("\${qiniu.ak}")
    private lateinit var ak: String

    @Value("\${qiniu.sk}")
    private lateinit var sk: String


    fun generData(method: String,path:String,raqQuery:String?,host:String,contentType:String?,bodyStr:String?):String{

        var data = "$method $path"
        if(raqQuery!=null){
            data += "?$raqQuery"
        }
        data += "\nHost: $host"
        if(contentType!=null){
            data += "\nContent-Type: $contentType"
        }
        data += "\n\n"
        val bodyOk = contentType!=null && bodyStr != null
        val contentTypeOk = contentType!=null && contentType != "application/octet-stream"
        if(bodyOk && contentTypeOk){
            data += bodyStr
        }
        Console.log(data)
        return data
    }

    //生成token
    fun auchCode(data: String): String{
       val encodedSign = base64Encode(hmac(data))
        return "Qiniu $ak:$encodedSign";
    }

    //hmac编码
    private fun hmac(value: String) : String {
        return HmacUtils(HmacAlgorithms.HMAC_SHA_1,sk).hmacHex(value)
    }

    //base64编码
    private fun base64Encode(value: String):String{
       return Base64.encode(value)
    }

使用

@RestController
@RequestMapping("/api")
class QiNController : QiNBaseController(){


    @GetMapping("/qn/straeams")
    fun getStreams() :ResultJson<String>{
        val host = "pili.qiniuapi.com"
        var url = "/v2/hubs/girlmerry/streams"
        val contentType = "application/x-www-form-urlencoded"
        val data = generData(
            method = "GET",
            bodyStr = null,
            host = host,
            path = url,
            raqQuery = null,
            contentType = contentType
        )
        val authorization = auchCode(data)
        print(authorization)
        val get = HttpRequest.get("https://$host$url").header("Authorization",authorization)
        return ResultJson.SUCCESS(get.execute().body())
    }


}

image

可以帮忙看看签名哪里错了吗?