我正在尝试编写一个VBA宏,它会将我的凭据传递给一个地址并获取一些内容(用于JIRA的REST API),但是我在将代码从java转换为VBA时遇到了一些困难。目前,这是我的java代码:
String username = "myUser";
String password = "myPassword";
String authString = username + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
URL url = new URL(address);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic "
+ authStringEnc);
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
我正在尝试将其转换为VBA,我不完全确定如何处理它,或者是否有一些库可以促进这一点。
答案 0 :(得分:3)
对于基本身份验证,您可以简单地:
Dim response As String
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", address, false, username, password
.Send
response = .responseText
End With
Msgbox response
答案 1 :(得分:2)
使用“授权”请求标头。 “授权”请求标头需要用户名和密码的加密字符串。
.setRequestHeader "Authorization", "Basic " & EncodeBase64
所以这里JiraService
是一个XMLHTTP对象,这样的东西将进行身份验证,其中EncodeBase64
是一个返回加密字符串的函数。
Public Function isAuthorized() As Boolean
With JiraService
.Open "POST", sURL & "/rest/api/2/issue/", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "Authorization", "Basic " & EncodeBase64
.send ""
If .Status <> 401 Then
isAuthorized = True
Else
isAuthorized = False
End If
End With
Set JiraService = Nothing
End Function
答案 2 :(得分:1)
p = Popen(['command'], stdout=PIPE, universal_newlines=True)
result = []
for line in p.stdout:
if 'stuff' in line:
value = line.split('delimiter')[4]
result.append(value)