老师,我改到下面这样子了,一直提示"远程服务器返回错误: (401) 未经授权。",
麻烦老师帮我看下是哪里出错了可以吗?谢谢啦
AK,SK,robotId都是真实的
Dim AK As String: AK = "1111"
Dim SK As String: SK = "222"
Dim host As String : host = "localhost:8080"
Dim method As String : method = "POST"
Dim URI As String : URI = "/api/test"
' 计算x_bce_date和authStringPrefix
Dim x_bce_date As String: x_bce_date = Format(Date.Now, "yyyy-MM-ddTHH:mm:ssZ")
Output.Show(x_bce_date)
Dim authStringPrefix As String: authStringPrefix = "cc-api-auth-v1/" & AK & "/" & x_bce_date & "/1800"
' 计算CanonicalRequest
'Dim CanonicalRequest As String: CanonicalRequest = method & vbCrLf & URI & vbCrLf & "" & vbCrLf & "host:" & host & vbCrLf & "host" & vbCrLf & "UNSIGNED-PAYLOAD"
Dim CanonicalRequest As String: CanonicalRequest = method & "\n" & URI & "\n" & "" & "\n" & "host:" & host & "\n" & "host" & "\n" & "UNSIGNED-PAYLOAD"
' 计算signingKey和Signature
Dim signingKey As Object
signingKey = CreateObject("System.Security.Cryptography.HMACSHA256")
signingKey.Key = System.Text.Encoding.Unicode.GetBytes(SK)
Dim authStringPrefixBytes As Object
authStringPrefixBytes = System.Text.Encoding.Unicode.GetBytes(authStringPrefix)
signingKey.ComputeHash( authStringPrefixBytes)
Dim signingKeyHash As Byte(): signingKeyHash = signingKey.Hash
Dim Signature As Object
Signature = CreateObject("System.Security.Cryptography.HMACSHA256")
Signature.Key = signingKeyHash
Dim CanonicalRequestBytes As Byte(): CanonicalRequestBytes = System.Text.Encoding.Unicode.GetBytes(CanonicalRequest)
Signature.ComputeHash (CanonicalRequestBytes)
Dim SignatureHash As Byte(): SignatureHash = Signature.Hash
' 生成Authorization并放到header里
Dim Authorization As String: Authorization = authStringPrefix & "/host/"
For i As Integer = 0 To UBound(SignatureHash)
Authorization = Authorization & Right("0" & Hex(SignatureHash(i)), 2)
Next
output.show("token=" & Authorization)
Dim hc As New HttpClient("https://aicc.bce.baidu.com/api/v3/console/realtime/status/create")
hc.Conte ntType = "application/json" '
hc.FormData.Add("Authorization",Authorization)
Dim jo As New JObject
jo("robotId") = CStr("333")
jo("mobile") = CStr("12345678901")
jo("secretType") = CStr("2")
Dim ja As New JArray
jo("dialogVar") = ja
ja.Add(New JObject) '给数组添加两个对象成员
ja(0)("xqmc") = CStr("小区")
ja(0)("khmc") = CStr("张先生")
ja(0)("rwsj") = CStr("下午三点")
ja(0)("rwnr") = CStr("带看")
hc.Content = jo.ToString
Dim ret As String = hc.GetData
Output.Show( "识别结果JSON: " & vbcrlf & ret)
[此贴子已经被作者于2023/4/21 13:27:26编辑过]