在VBA中发送JSON POST请求

时间:2018-01-07 01:52:27

标签: json excel vba rest api

我有以下JSON POST示例代码,我试图将其转换为VBA for Excel:

POST /services/shipper/orders HTTP/1.1
Content-Type: application/json
User-Agent: Mozilla 5.0
Host: qa.etowertech.com
X-WallTech-Date: Tue, 06 Jan 2018 21:20:27 GMT
Authorization: WallTech test5AdbzO5OEeOpvgAVXUFE0A:79db9e5OEeOpvgAVXUFWSD

以下是我提出的代码:

With JsonHTTP
             .Open "POST", "http://qa.towertech.com/services/shipper/orders", False
             .setRequestHeader "RequestName", "application/json"
             .setRequestHeader "Accept", "application/json"
             .setRequestHeader "User-Agent", "Mozilla 5.0"
             .setRequestHeader "Host", "qa.etowertech.com"
             .setRequestHeader "X-WallTech-Date", "Tue, 06 Jan 2018 21:20:27 GMT"
             .setRequestHeader "Authorization", "WallTech test5AdbzO5OEeOpvgAVXUFE0A:79db9e5OEeOpvgAVXUFWSD"
             .send (body)
End With

我不确定POST /services/shipper/orders HTTP/1.1输入应该去哪里

我一直得到这样的答复:

{
  "status": "Failed",
  "errors": [
    {
      "code": 100004,
      "message": "System internal error"
    }
  ],
  "data": null
}

修改

我得到的当前回应:

{
  "status": "Failed",
  "errors": [
    {
      "code": 401,
      "message": "Authorization information is invalid."
    }
  ],
  "data": null
}

我只是觉得我错过了签名(见下面的说明),但不确定如何渲染它以及它在请求中的确切位置?

X-华尔顿-日期

EEE, dd MMM yyyy HH:mm:ss zzz

授权

WallTech <Access Token>:<Base64 Encoded HMAC SHA-1 Hash>

签名字符串

<HTTP Verb> + "\0x000A" + <X-WallTech-Date Header> + "\0x000A" + <Full URL>

1 个答案:

答案 0 :(得分:1)

以下是解决方案:

url = "http://qa.towertech.com/services/shipper/orders"
token = "xxxxxx" ' API token goes here
auth = Base64_HMACSHA1("POST" & Chr(10) & timestamp & Chr(10) & url, key)

With JsonHTTP
             .Open "POST", url, False
             .setRequestHeader "RequestName", "application/json"
             .setRequestHeader "Accept", "application/json"
             .setRequestHeader "User-Agent", "Mozilla 5.0"
             .setRequestHeader "Host", "qa.etowertech.com"
             .setRequestHeader "X-WallTech-Date", timestamp
             .setRequestHeader "Authorization", "Walltech " & token & ":" & auth
             .send (body)
End With

问题是Base64函数中缺少密钥,因此auth字符串编码错误

希望这可以帮助其他人拥有相同的东西!