VBA httpWebRequest with Credentials

时间:2018-04-17 20:49:23

标签: excel-vba vba excel

我需要从云服务中获取一些数据。

到目前为止,这是我的代码:

for-each

没有凭据,它给了我“401 - 未经授权” - 响应,这是好的,我想。

但是我确实拥有必要的凭据,但似乎无法解决Option Explicit Sub Test() Call GetHTTPResult("https://venice.unit4.com/WebConnect/api/ZZKlesserRob/2017/Balance/GetBalance?AccountNumber=604&BeginMonth=1&EndMonth=1", "username", "password") End Sub Function GetHTTPResult(sURL As String, Optional username As String, Optional password As String) As String Dim XMLHTTP As Object, sResult As String Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") username = "dummy" password = "dummypass" XMLHTTP.Open "GET", sURL, False XMLHTTP.setRequestHeader "Authorization", "Basic" & Base64Encode("username" & ":" & "password") XMLHTTP.Send Debug.Print "Status: " & XMLHTTP.Status & " - " & XMLHTTP.StatusText sResult = XMLHTTP.ResponseText GetHTTPResult = sResult Set XMLHTTP = Nothing End Function 编码带来的问题。

我该怎么办?

提前致谢!

1 个答案:

答案 0 :(得分:0)

通过检查docs,我相信您的space标题中缺少Authorization

var request = new XMLHttpRequest();
...
request.setRequestHeader("Accept","application/xml");
request.setRequestHeader("Authorization","Basic " + Base64.encode ("User:Password"));

所以你应该阅读:

XMLHTTP.setRequestHeader "Authorization", "Basic " & Base64Encode(username & ":" & password)

此外,您应该使用Acceptapplication/xml为请求添加application/json标头,以指定所请求数据的格式。