创建一个 Excel VBA,它将获取凭据并从 URL 带来访问令牌(授权令牌),然后将其作为标题部分附加到另一个 URL 中,以便为给定网站(cyber-ark)创建条目。
尝试使用如下:
Sub Main()
Dim objRequestForToken As Object
Dim objRequestForAddingAccount As Object
Dim strURL As String
Dim binAsync As Boolean
Dim strResponse As String
Dim AllBody As String
Set objRequestForToken = CreateObject("MSXML2.XMLHTTP")
strURL = "URL to get token"
binAsync = True
AllBody = "{""username"":""UserID"",""password"":""Password""}"
With objRequestForToken
.Open "POST", strURL, binAsync
.SetRequestHeader "Content-Type", "application/json"
.Send (AllBody)
While objRequestForToken.readystate <> 4
DoEvents
Wend
strResponse = .responseText
Debug.Print (strResponse)
End With
Set objRequestForAddingAccount = CreateObject("MSXML2.XMLHTTP")
strURL = "URL to make an entry"
AllBody = "{""propeties"" = ""null"" ""sample"" = ""sample""; ""secretType"" = ""password""}"
With objRequestForAddingAccount
.Open "POST", strURL, binAsync
.SetRequestHeader "Authorization", strResponse
.SetRequestHeader "Content-Type", "application/json"
.Send (AllBody)
While objRequestForAddingAccount.readystate <> 4
DoEvents
Wend
strResponse = .responseText
Debug.Print (strResponse)
End With
End Sub
但是,它正确地提供了令牌值,但是,在第二个 URL 调用中将令牌作为标头值附加时,它返回了一个错误:
{"ErrorCode":"PASWS006E","ErrorMessage":"会话令牌丢失、无效或过期。"}
谁能解释为什么它显示为令牌过期?PowerShell 脚本中的同一段代码使用 Invoke-Rest API 值正常工作。