Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mdddj opened this issue Mar 10, 2022 · 0 comments
Open

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

mdddj opened this issue Mar 10, 2022 · 0 comments

Comments

@mdddj
Copy link

mdddj commented Mar 10, 2022

这是我的签名函数

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant