对Jira Rest API的新手...我想使用VBA程序登录到本地Jira服务器。我可以登录并手动使用带有URL的REST API并获得我正在寻找的JSON响应,但是每次我尝试使用VBA(Excel)来执行此操作时,都会出现以下错误
{“ errorMessages”:[“您无权查看指定的问题。”,“需要登录”],“错误”:{}}
我正在使用基本授权。还有什么我需要做的吗? (我正在使用下面可以从其他帖子复制的代码)。也许Jira会将我的代码视为与登录名不同的用户?
Sub JiraRestGetCall()
Call JiraRest
End Sub
Private Function UserPassBase64() As String
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement
Dim arrData() As Byte
arrData = StrConv("MyUserName:MyPassword", vbFromUnicode)
Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
UserPassBase64 = objNode.Text
End Function
Public Function JiraRest()
Dim JiraService As New MSXML2.XMLHTTP60
With JiraService
.Open "Get", "http://localhost:8080/rest/api/2/issue/NP-1.json", False
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "Authorization", "Basic " & UserPassBase64
.Send ""
If .Status = "401" Then
MsgBox "Not authorized of invalid username/password"
Else
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "Get", "http://localhost:8080/rest/api/2/issue/NP-1.json"
MyRequest.Send
MsgBox MyRequest.ResponseText
End If
End With
End Function
它的工作原理:
Posting
一个新的Jira会话,包括您的登录信息,并从响应中提取会话ID。
Getting
所需的任何数据,包括标题中的会话IDDeleting
会话Gets
,则可以将其拆分为3个独立的函数,并且仅运行步骤1和3。Function getJiraJSON(restURL As String, requestString As String, jiraUsername As String, jiraPassword As String) As String
Dim JiraService As New MSXML2.XMLHTTP60
Dim JiraAuth As New MSXML2.XMLHTTP60
Dim postResp As String
Dim cookieVal As String
With JiraAuth
.Open "POST", restURL & "auth/1/session", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "X-Atlassian-Token", "no-check"
.send " {""username"" : """ & jiraUsername & """, ""password"" : """ & jiraPassword & """}"""
postResp = .responseText
cookieVal = "JSESSIONID=" & Mid(postResp, 42, 32) & "; Path=/Jira" 'Extract the Session-ID
End With
With JiraService
.Open "GET", restURL & requestString, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "Set-Cookie", cookieVal
.send
getJiraJSON = .responseText
End With
With JiraAuth
.Open "DELETE", restURL & "auth/1/session", False
.send
End With
End Function
在您的情况下,您可以使用:
MsgBox getJiraJSON("http://localhost:8080/rest/", "api/2/issue/NP-1.json", "myUsername", "myPassword")