首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用VBA Excel 2010访问JIRA

使用VBA Excel 2010访问JIRA
EN

Stack Overflow用户
提问于 2017-05-23 23:56:14
回答 1查看 4.5K关注 0票数 2

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

代码语言:javascript
运行
复制
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的新手

EN

回答 1

Stack Overflow用户

发布于 2020-10-06 19:09:29

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

代码语言:javascript
运行
复制
'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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44139684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档