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,所以只能手动写, 感谢!!