以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 腾讯云的签名API (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=194832) |
-- 作者:rayfux3322 -- 发布时间:2025/1/18 17:34:00 -- 腾讯云的签名API 您好,老师, 请教一下下面这个腾讯云的签名,我调了好多回都在提示错误, 但我又找不到原因,请帮忙看看.谢谢 签名文档如下: https://cloud.tencent.com/document/api/598/38504文生图文档如下: 1.返回来的错误提示说键值要用=号分开: {"Response":{"Error":{"Code":"InvalidParameter","Message":"Url key and value should be splited by `=`."},"RequestId":"a6faa7d0-dbed-46e0-b1bf-8c79c1d744cd"}} 2.我的代码分两部分, 一部分是全局代码,一部分是按钮代码: 2.1 全局代码: Public Function SHA256Hex(ByVal s As String) As String Using algo As System.Security.Cryptography.SHA256 = System.Security.Cryptography.SHA256.Create() Dim hashbytes As Byte() = algo.ComputeHash(Encoding.UTF8.GetBytes(s)) Dim builder As StringBuilder = New StringBuilder() For i As Integer = 0 To hashbytes.Length - 1 builder.Append(hashbytes(i).ToString("x2")) Next Return builder.ToString() End Using End Function Public Function HmacSHA256(ByVal key As Byte(), ByVal msg As Byte()) As Byte() Using mac As System.Security.Cryptography.HMACSHA256 = New System.Security.Cryptography.HMACSHA256(key) Return mac.ComputeHash(msg) End Using End Function Public Function BuildHeaders(ByVal secretid As String, ByVal secretkey As String, ByVal service As String, ByVal endpoint As String, ByVal region As String, ByVal action As String, ByVal version As String, ByVal Date2 As DateTime, ByVal requestPayload As String) As Dictionary(Of String, String) Dim datestr As String = Date2.ToString("yyyy-MM-dd") Dim startTime As DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) Dim requestTimestamp As Long = CLng(Math.Round((Date2 - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero)) / 1000 \'\'Timestamp 必须是当前系统时间,且需确保系统时间和标准时间是同步的,如果相差超过五分钟则必定失败 Dim algorithm As String = "TC3-HMAC-SHA256" \'---------------------------------------------- Dim httpRequestMethod As String = "POST" Dim canonicalUri As String = "/" Dim canonicalQueryString As String = "" Dim contentType As String = "application/json" Dim canonicalHeaders As String = "content-type:" & contentType & "; charset=utf-8" & vbLf & "host:" & endpoint & vbLf & "x-tc-action:" & action.ToLower() & vbLf Dim signedHeaders As String = "content-type;host;x-tc-action" Dim hashedRequestPayload As String = SHA256Hex(requestPayload) Dim canonicalRequest As String = httpRequestMethod & vbLf & canonicalUri & vbLf & canonicalQueryString & vbLf & canonicalHeaders & vbLf & signedHeaders & vbLf & hashedRequestPayload Console.WriteLine(canonicalRequest) Dim credentialScope As String = datestr & "/" & service & "/" & "tc3_request" Dim hashedCanonicalRequest As String = SHA256Hex(canonicalRequest) Dim stringToSign As String = algorithm & vbLf & requestTimestamp.ToString() & vbLf & credentialScope & vbLf & hashedCanonicalRequest Console.WriteLine(stringToSign) Dim tc3SecretKey As Byte() = Encoding.UTF8.GetBytes("TC3" & secretkey) Dim secretDate As Byte() = HmacSHA256(tc3SecretKey, Encoding.UTF8.GetBytes(datestr)) Dim secretService As Byte() = HmacSHA256(secretDate, Encoding.UTF8.GetBytes(service)) Dim secretSigning As Byte() = HmacSHA256(secretService, Encoding.UTF8.GetBytes("tc3_request")) \'--- Dim signatureBytes As Byte() = HmacSHA256(secretSigning, Encoding.UTF8.GetBytes(stringToSign)) \'--- Dim signature As String = BitConverter.ToString(signatureBytes).Replace("-", "").ToLower() Console.WriteLine(signature) \'-------------------------------------------------- Dim authorization As String = algorithm & " " & "Credential=" & secretid & "/" & credentialScope & ", " & "SignedHeaders=" & signedHeaders & ", " & "Signature=" & signature Console.WriteLine(authorization) \'-------------------------------------------------- Dim headers As Dictionary(Of String, String) = New Dictionary(Of String, String)() headers.Add("Authorization:", authorization) headers.Add("Content-Type:", contentType & "; charset=utf-8") headers.Add("Host:", endpoint) headers.Add("X-TC-Action:", action) headers.Add("X-TC-Timestamp:", requestTimestamp.ToString()) headers.Add("X-TC-Version:", version) headers.Add("X-TC-Region:", region) Return headers End Function 2.2是按钮代码: Dim startTime As DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) Dim requestTimestamp As Long = CLng(Math.Round((Date.now - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero)) / 1000 Dim secretid As String = "***********************" \' 这个id和key,需要的话我可提供. Dim secretkey As String = "***********************" Dim service As String = "hunyuan" Dim endpoint As String = "hunyuan.tencentcloudapi.com" Dim region As String = "ap-guangzhou" Dim action As String = "SubmitHunyuanImageJob" \'"DescribeInstances" Dim version As String = "2023-09-01" Dim Date2 As DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(requestTimestamp) \'1736985600 \'1551113065 Dim requestPayload As String = "{""Prompt"": ""雨中, 竹林, 小路""}" Dim headers As Dictionary(Of String, String) = BuildHeaders(secretid, secretkey, service, endpoint, region, action, version, Date2, requestPayload) Dim htc As New HttpClient("https://hunyuan.tencentcloudapi.com") htc.C Dim kk As String For Each key As String In headers.Keys htc.Headers.Add(key, headers(key)) Next For Each key2 As String In headers.Keys kk = kk & (key2 & headers(key2) & vbCrLf) Next htc.Content = requestPayload Dim ret As String = htc.GetData() MessageBox.Show(ret) MessageBox.Show(kk) 请帮忙分析一下, 因为他们没有.net4.0的SDK,所以只能手动写, 感谢!! |
-- 作者:有点蓝 -- 发布时间:2025/1/19 20:32:00 -- 把vblf全部改为字符串的“\\n”试试 Dim canonicalHeaders As String = "content-type:" & contentType & "; charset=utf-8\\nhost:" & endpoint & "\\nx-tc-action:" & action.ToLower() & "\\n" 其它地方也这样改改试试
|
-- 作者:rayfux3322 -- 发布时间:2025/1/20 10:21:00 -- 回复:(有点蓝)把vblf全部改为字符串的“\n”试试&n... 您好有点蓝老师, 我测试过,vblf的代码是可以与示例中的值对上的, \\n对不上, 所以应该还是vblf合适. 请帮忙再指点一下, 谢谢 |
-- 作者:有点蓝 -- 发布时间:2025/1/20 10:28:00 -- 代码看不出有什么问题。建议联系接口方客服,查查提交的数据什么地方有问题 文档好像提供了一个什么API Explorer,试试
|
-- 作者:rayfux3322 -- 发布时间:2025/1/20 14:11:00 -- 客户说不熟悉我的代码,调不了, 头痛, |
-- 作者:rayfux3322 -- 发布时间:2025/1/20 14:21:00 -- 我验证了一下函数SHA256算出来的结果对得上示例, 按示例的拼接方法算的SHA256值也对得上, 说明拼接方法没有问题的, 只有当加上authorization这个头部信息后, 就提示"Url key and value should be splited by `=`."了, 所以,感觉还是困惑, 因为一个接口就这点内容.
|
-- 作者:有点蓝 -- 发布时间:2025/1/20 14:35:00 -- 那就安装vs,测试他们的c#代码是否正常 |
-- 作者:rayfux3322 -- 发布时间:2025/1/20 14:40:00 -- 谢谢了. |