【问题标题】:Access JIRA with VBA Excel 2010使用 VBA Excel 2010 访问 JIRA
【发布时间】:2017-05-23 15:56:13
【问题描述】:

我想使用 VBA 和 Excel 在 JIRA 中自动生成 Excel 报告,我正在尝试使用 VBA 访问 JIRA 以将数据导出到 Excel。因此,我从身份验证开始,然后尝试使用此代码导出数据:

Private JiraService As New MSXML2.XMLHTTP60
Private JiraAuth As New MSXML2.XMLHTTP60

Sub JIRA()

  With JiraAuth
       .Open "POST", "https://jiralink/rest/auth/1/session", False
       .setRequestHeader "Content-Type", "application/json"
       .setRequestHeader "Accept", "application/json"
       .send " {""username"" : """username""", ""password"" : """password"""}"""
       MsgBox .Status
       If .Status = "200" Then
           sCookie = "JSESSIONID=" & Mid(sErg, 42, 32) & "; Path=/" & sPfad
           Login = True
       End If
  End With

  With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;https://jiralink/sr/jira.issueviews:searchrequest-excel-all-fields/temp/SearchRequest.html?jqlQuery=project+%3D+NAME+AND+Sprint+%3D+1+ORDER+BY+priority+DESC%2C+updated+DESC&tempMax=1000" _
        , Destination:=Range("$A$1"))
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

End Sub

我收到错误:403 禁止 谁能帮帮我? 抱歉,我是 VBA 和 JIRA 的新手

【问题讨论】:

  • 您是否在send 请求中输入了正确的用户名和密码?同样,仅仅因为您已通过 Web 服务登录 JIRA 并不意味着查询连接提供了正确的登录凭据。
  • 感谢您的重播@Scott Holtzman,是的,用户名和密码正确
  • 我想连接到我的 JIRA 帐户,然后访问允许我将数据导出到 Excel 的 URL jiralink/sr/jira.issueviews:searchrequest-excel-all-fields/temp/…>

标签: vba excel jira jira-rest-api


【解决方案1】:

我用它从我们的 Jira 服务器中提取信息,我正在使用来自 https://github.com/VBA-tools/VBA-JSON 的 JsonConverter 来获取从 Json 转换而来的数据:

'Option Explicit
Dim jiraURL, Auth, restReq
Dim Url, responseCode, responseContent
Sub startFunction()
Dim loginOk As Boolean

loginOk = PrepareLogin

If (loginOk = True) Then
    getIssues 'Login ok, now fetch all issues
End If
MsgBox "All done"
End Sub
Sub getIssues()
Dim wsStartPage As Worksheet
Dim jsonResp As Object
Dim currentRow As Long
Dim issueName As String
Dim i As Long
Dim moreJiratoRead As Boolean
Dim jiraPosition As Long
    
currentRow = 1

Set wsStartPage = ActiveWorkbook.Sheets("MyIssues")
wsStartPage.Cells(currentRow, 1) = "Issue Name"
currentRow = currentRow + 1
moreJiratoRead = True
jiraPosition = 0
    
Do While (moreJiratoRead = True)

    Url = jiraURL & "rest/api/latest/search?jql=project in (someProjectNames) and status not in (dismissed, done, closed, resolved)&startAt=" & CStr(jiraPosition) & "&maxResults=1000&expand=fields&fields=key,status"
    restReq.Open "GET", Url, False
    restReq.setRequestHeader "Authorization", "Basic " & Auth
    restReq.setRequestHeader "Content-Type", "application/json"
    restReq.Send
    responseCode = restReq.Status
    responseContent = restReq.responseText
    Set jsonResp = JsonConverter.ParseJson(responseContent)
    Set issues = jsonResp("issues")

    If issues.Count = 1000 Then 'This will allow me to read all issues without limitation
        jiraPosition = jiraPosition + 1000
    Else
        moreJiratoRead = False
    End If

    
    i = 1
    Do While i <= issues.Count
        issueName = CStr(issues(i)("key"))
        wsStartPage.Cells(currentRow, 1) = issueName
        currentRow = currentRow + 1
        i = i + 1
    Loop
Loop
End Sub

Function PrepareLogin() As Boolean
Dim uid, pwd

PrepareLogin = False
uid = InputBox("Please enter your windows login id", "Windows Login")
If Len(Trim(uid)) = 0 Then
    Exit Function
Else
    pwd = InputBox("Please enter your windows password", "Windows Password")
    If Len(Trim(pwd)) = 0 Then
        Exit Function
    End If
End If

PrepareLogin = init(uid, pwd)

End Function

Function init(JIRA_USER, JIRA_PWD) As Boolean
'Login and get session ID.
Dim sOutput
Dim sCookie

init = True
jiraURL = "https://someURL/"
Set restReq = CreateObject("Msxml2.ServerXMLHTTP.6.0")
restReq.setTimeouts 6000000, 6000000, 6000000, 6000000

restReq.Open "POST", jiraURL & "/rest/auth/1/session", False
restReq.setRequestHeader "Content-Type", "application/json"
restReq.setRequestHeader "Accept", "application/json"
restReq.Send " {""username"" : """ & JIRA_USER & """, ""password"" : """ & JIRA_PWD & """}"
sOutput = restReq.responseText

If (InStr(1, LCase(sOutput), LCase("login failed")) Or InStr(1, LCase(sOutput), LCase("unauthorized"))) > 0 Then
    init = False
    MsgBox sOutput
    End
End If
sCookie = "JSESSIONID=" & Mid(sOutput, 42, 32) & "; Path=/Jira"

End Function

【讨论】:

  • 您需要将 someProjectNames 替换为 Jira 中的一些项目名称,并且您需要将 someURL 替换为您的 Jira 服务器,即 jira.company.com,并且您需要安装我提到的 JsonConverter 来转换反馈您从 Jira API 获得。
猜你喜欢
  • 1970-01-01
  • 2016-03-17
  • 2022-12-19
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
相关资源
最近更新 更多