Unable to login on jira api using vba

deepak.sirsale December 22, 2019

Hi Guys,

I am trying fetch comments data for issues using JIRA API's but not able to do so as I am not able to authenticate/login.

 

I am using below code, Plz assist if I am making any mistake :

 

Sub JIRA()

Dim oJiraAuth As MSXML2.ServerXMLHTTP60
Dim oJiraService As MSXML2.ServerXMLHTTP60
Dim JIRA_USER As String, JIRA_PWD As String

' User id is the id shown in profile JIRA link https://Jiradomain/jira/people/xyz

JIRA_USER = "xyz" 
JIRA_PWD = "abc$" ' Generated the token and using token number as password

Set oJiraAuth = New MSXML2.ServerXMLHTTP60
With oJiraAuth
.Open "POST", "https://iradomain/rest/auth/1/session", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.send " {""username"" : """ & JIRA_USER & """, ""password"" : """ & JIRA_PWD & """}"
sOutput = .responseText
sCookie = "JSESSIONID=" & Mid(sOutput, 42, 32) & "; Path=/Jira"
End With


Set oJiraService = New MSXML2.ServerXMLHTTP60
With oJiraService
.Open "GET", "https://Jiradomain/rest/api/2/issue/AB-1/comment", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "Authorization", "Basic " & EncodeBase64(JIRA_USER & ":" & JIRA_PWD)
'.setRequestHeader "Set-Cookie", sCookie
.send
sOutput = .responseText
sStatus = .Status & " | " & .statusText
End With

'sOutput should contain all data in JSON format
'next, you must use a JSON parser to get the desired format

Set oJiraService = Nothing
Set oJiraAuth = Nothing
End Sub


Private Function EncodeBase64(srcTxt As String) As String
Dim arrData() As Byte
arrData = StrConv(srcTxt, vbFromUnicode)
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement

Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData

EncodeBase64 = objNode.text

Set objNode = Nothing
Set objXML = Nothing
End Function

1 answer

0 votes
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2019

Hi @deepak.sirsale 

I think your problem is that you're using your User Id, it should be the Jira e-mail that you use to login

deepak.sirsale December 23, 2019

Thanks @Warren  I have tried using my email id which I use to login JIRA and now getting error code as 403 and error description as "XSRF check failed", Any clue why this is happening please.

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2019

Hi @deepak.sirsale 

I would suggest that you try using Postman (either online or download the app) to prove that you can successfully authorise. If what you're trying to do in Postman works, then you know that your login details are good and you can recheck your code.

In the meantime I'll try your code in Excel with my login details and URL to see if I can spot anything

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2019

Hi @deepak.sirsale 

Your EncodeBase64 function has an issue, but I'm not sure what.

When I run through your code and check the value that is returned from EncodeBase64, it has a carriage return character in it which shouldn't be there. I go to the Immediate window and type

?EncodeBase64 

and the result is returned across 2 lines.

I copied that into a text editor, removed the line break, copied the result back into the EncodeBase64 variable and it then works.

You need to try and track down why you're getting that character

Like Brian Wooldridge likes this
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2019

Hi @deepak.sirsale 

I just tried using a different function - see improved function which works for me

Suggest an answer

Log in or Sign up to answer