VBA to download macro enabled file (xlsm) from Jira - vba

I am trying to download a macro enabled file (xlsm) from Jira, I have tried the below method and able to download the file but the file is corrupted. Please advice what I am missing here.
Dim myURL As String
myURL = "Put your download link here"
Dim HttpReq As Object
Set HttpReq = CreateObject("Microsoft.XMLHTTP")
HttpReq.Open "GET", myURL, False, "username", "password"
HttpReq.send
myURL = HttpReq.responseBody
If HttpReq.Status = 200 Then
Set oStrm = CreateObject("ADODB.Stream")
oStrm.Open
oStrm.Type = 1
oStrm.Write HttpReq.responseBody
oStrm.SaveToFile ThisWorkbook.Path & "\" & "file.xlsm", 2 ' 1 = no overwrite, 2 = overwrite
oStrm.Close
End If
(source)

Finally after several attempts, below code work, hope this is helpful for others.
Private Sub DownloadFromJira()
Dim oJiraService As MSXML2.ServerXMLHTTP60
Dim sPath As String
Dim sStatus As String
Dim FileData() As Byte
Dim FileNum As Long
Set oJiraService = New MSXML2.ServerXMLHTTP60
sPath = "C:\Users\**ID**\Downloads\Test.xlsm"
With oJiraService
.Open "GET", "https://**MyJiraLink**/secure/attachment/123455/Test.xlsm", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Basic " & EncodeBase64(JiraUID & ":" & JiraPWD)
.setRequestHeader "Accept", "application/json"
.send
sStatus = .status & " | " & .statusText
If .status = "401" Then
MsgBox "Not Connected"
End If
FileData = .responseBody
FileNum = FreeFile
Open sPath For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum
End With
Set oJiraService = 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

Related

Word VBA Rest API Simplybook.me Getting 401 Unauthorized

Below code is where I get 401 Unautorized access error After 2nd request Rest API is called .
Here is the developer Rest API documentation supplied.
https://simplybook.me/en/api/developer-api/tab/rest_api#method_GET_/admin/bookings/{id}
Sub Test()
Dim objRequest As Object
Dim loginResponse As Object
Dim strUrl As String
Dim bookingcode As String
Dim blnAsync As Boolean
Dim strloginResponse, strBookingresponse, strlogoutresponse, strparameter As String
Dim scriptControl As Object
Set scriptControl = CreateObject("MSScriptControl.ScriptControl")
scriptControl.Language = "JScript"
Set objRequest = CreateObject("MSXML2.XMLHTTP")
'strUrl = "https://user-api.simplybook.me/admin/auth"
strUrl = "https://user-api-v2.simplybook.me/admin/auth"
blnAsync = True
'strUrl = "https://user-api.simplybook.me/admin/auth"
strUrl = "https://user-api-v2.simplybook.me/admin/auth"
With objRequest
.Open "POST", strUrl, blnAsync
.SetRequestHeader "Content-Type", "application/json"
.Send ("{""company"":""simplydemo"",""login"":""admin"",""password"":""demo""}")
'spin wheels whilst waiting for response
While objRequest.readyState <> 4
DoEvents
Wend
strloginResponse = .responsetext
End With
Set loginResponse = scriptControl.Eval("(" + strloginResponse + ")")
Debug.Print loginResponse.token
Debug.Print loginResponse.company
Debug.Print loginResponse.login
Debug.Print loginResponse.refresh_token
Debug.Print loginResponse.domain
Debug.Print loginResponse.require2fa
Debug.Print loginResponse.allowed2fa_providers
Debug.Print loginResponse.auth_session_id
bookingcode = "0bz29vl4t"
strUrl = "https://user-api-v2.simplybook.me/admin/bookings/" & bookingcode
'strUrl = "https://user-api.simplybook.me/admin/bookings/" & bookingcode
strparameter = "(""{""X-Company-Login"":""" & loginResponse.company & """,""X-Token"":""" & loginResponse.token & """}"")"
Debug.Print strparameter
Set objRequest = CreateObject("MSXML2.XMLHTTP")
With objRequest
.Open "GET", strUrl, blnAsync
.SetRequestHeader "Content-Type", "application/json"
.Send strparameter
'spin wheels whilst waiting for response
While objRequest.readyState <> 4
DoEvents
Wend
strBookingresponse = .responsetext
End With
Debug.Print strBookingresponse
Set objRequest = CreateObject("MSXML2.XMLHTTP")
strUrl = "https://user-api-v2.simplybook.me/admin/auth/logout"
strparameter = "(""{""auth_token"":""" & loginResponse.token & """}"")"
With objRequest
.Open "POST", strUrl, blnAsync
.SetRequestHeader "Content-Type", "application/json"
.Send strparameter
'spin wheels whilst waiting for response
While objRequest.readyState <> 4
DoEvents
Wend
strlogoutresponse = .responsetext
End With
Debug.Print strBookingresponse
Set scriptControl = Nothing
Set objRequest = Nothing
Set loginResponse = Nothing
End Sub
Below response is i get for the Debug.print statements:
4eb8b7f6ce820c24e66a754557635ede02e6e808ce27e311511a20bef4ed44e3
simplydemo
admin
b60ee1e89264ee5e856583fc20e63926e71a25bded9f9a540d12d5ccbf51bf51
simplybook.me
False
("{"X-Company-Login":"simplydemo","X-Token":"4eb8b7f6ce820c24e66a754557635ede02e6e808ce27e311511a20bef4ed44e3"}")
{"code":401,"message":"Unauthorized","data":[],"message_data":[]}
{"code":401,"message":"Unauthorized","data":[],"message_data":[]}

Error message: "Operation value must be a number", on creation of new JIRA issue

When creating a new JIRA issue using REST API in VBA, I'm getting the following error message for custom field 13744: "Operation value must be a number".
I've tested the following code without the numeric field, and everything works fine, the issue is created and all the other fields value are passed:
Public Pass As Variant
Sub criarIssue()
Dim JiraService As New MSXML2.XMLHTTP60
Dim sErg As Variant
Dim sRestAntwort As Variant
Dim sSummary As Variant
Dim sDescription As Variant
Dim sProject As Variant
Dim sIssueType As Variant
Dim sData As Variant
Dim sPath As Variant
Dim sUsername As Variant
Dim sPassword As Variant
Dim sAux As Variant
Dim sStatus As Variant
Dim sEncbase64Auth As Variant
sUsername = Environ$("UserName")
Senha.Show
sPassword = Pass
sSummary = ActiveSheet.Range("C3").Value 'Issue name
sIssueType = "Projeto"
sProject = "EXP"
sData = " { ""fields"" : { ""project"" : { ""key"" : """ & sProject & """ }, ""summary"" : """ & _
sSummary & """, ""issuetype"" : { ""name"" : """ & sIssueType & """}, ""customfield_13744"" : ""1234"", ""customfield_13663"" : {""name"" : ""vitor.ribeiro""}, ""customfield_13670"" : ""2019-01-23 00:00:00"", ""customfield_13671"" : {""value"" : ""SP"" , ""child"" : {""value"" : ""SAO PAULO"" }}}} "
sEncbase64Auth = EncodeBase64(sUsername & ":" & sPassword)
'Creates Issue
With JiraService
.Open "POST", "http://172.16.2.128:8080/rest/api/2/issue/", False
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "Accept", "application/json"
.SetRequestHeader "X-Atlassian-Token", "nocheck"
.SetRequestHeader "Authorization", "Basic " & sEncbase64Auth
.Send (sData)
sRestAntwort = .ResponseText
sStatus = .Status & " | " & .StatusText
End With
' JSON Response
sAux = Replace(sRestAntwort, "{", "")
sAux = Replace(sAux, "}", "")
sAux = Split(sAux, ",")
sAux = sAux(1)
sAux = Split(sAux, ":")
sAux = sAux(1)
sAux = Replace(sAux, """", "")
ActiveSheet.Range("J2").Value = sStatus
ActiveSheet.Range("J3").Value = sRestAntwort & " | " & sAux
ActiveSheet.Range("J4").Value = ActiveSheet.Range("J4").Value + 1
ActiveSheet.Range("C2").Value = sAux
With JiraService
.Open "DELETE", "http://172.16.2.128:8080/rest/auth/1/session", False
.Send
End With
End Sub
Public Function EncodeBase64(text As String) As String
Dim arrData() As Byte
arrData = StrConv(text, vbFromUnicode)
Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.text
Set objNode = Nothing
Set objXML = Nothing
End Function
All but custom field 13744 values are passed without a problem.
For this one I get the following error message:
{"errorMessages":[],"errors":{"customfield_13744":"Operation value must be a number"}
Why isn't it recognized as a number?
It turns out I just had to pass de numeric value without quotes, as suggested by #cyboashu. It's working now.
You must delete punctuation mark "

Uploading an image to web with excel vba

I'm trying to upload an image to a REST webpage. I can do this successfully though a cURL call:
curl -u Admin:admin -T C:\temp\wikiTable.jpg http://192.168.0.35:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome/attachments/table.jpg
I'm now trying to achieve this though a HTTP Post in Excel vba but experiencing some problems. I'm currently doing this:
Const STR_BOUNDARY As String = "---------------------------123456789abc"
Dim nFile As Integer
Dim baBuffer() As Byte
Dim sPostData As String
Dim sFileName As String
Dim sUrl As String
sFileName = "C:\temp\wikiTable.jpg"
sUrl = "http://192.168.0.35:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome/attachments/table.jpg"
'--- read file
nFile = FreeFile
Open sFileName For Binary Access Read As nFile
If LOF(nFile) > 0 Then
ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
Get nFile, , baBuffer
sPostData = StrConv(baBuffer, vbUnicode)
End If
Close nFile
'-- post
Dim HTTPReq As Object
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
HTTPReq.Option(4) = 13056
HTTPReq.Open "Post", sUrl, False
HTTPReq.SetCredentials "Admin", "admin", 0
HTTPReq.setRequestHeader "Content-Type: multipart/form-data;"
HTTPReq.send sPostData
MsgBox (HTTPReq.responseText)
For the responseText I keep getting the following error:
10.4.6 405 Method Not Allowed
The method specified in the Request-Line is not allowed for the resource
identified by the Request-URI. The response MUST include an Allow header
containing a list of valid methods for the requested resource.
https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6
Any ideas what I'm doing wrong here?
The following worked in the end:
Private Function PostFile(sUrl As String, sFileName As String, strUserName As String, strPassword As String) As String
Dim nFile As Integer
Dim baBuffer() As Byte
Dim sPostData As String
'--- read file
Dim adoStream
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Mode = 3 ' read write
adoStream.Type = 1 ' adTypeBinary
adoStream.Open
adoStream.LoadFromFile (sFileName)
adoStream.Position = 0
'--- post
Dim HTTPReq As Object
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
HTTPReq.Option(4) = 13056
HTTPReq.Open "PUT", sUrl, False
HTTPReq.setRequestHeader "Authorization", "Basic " + Base64Encode(strUserName + ":" + strPassword)
HTTPReq.setRequestHeader "Content-Type", "multipart/form-data"
HTTPReq.setRequestHeader "Content-Length", adoStream.Size
HTTPReq.send (adoStream.Read(adoStream.Size))
pvPostFile = HTTPReq.responseText
Set adoStream = Nothing
End Function
I advise you try to call the method "POST" instead of "Post" because it might be case sensitive.

Parse/Scrape from Multiple Pages using XMLHTTP in VBA

The code below only scrapes/parses one page of data (50 results) into excel however, I would like it to scrape multiple pages (500 results) - please help. (Base64Encode was taken from a different source - I don't take credit for it)
`Function GetData() As Boolean
Application.ScreenUpdating = False
Dim objHTTP As New MSXML2.XMLHTTP
Dim strURL As String
Dim strUserName As String
Dim strPassword As String
Sheet1.Range("A2:R2000") = ""
Sheet1.Activate
strUserName = "User"
strPassword = "Password"
For i = 1 To UBound(MyArray)
strURL = "https://.ngx.com/ngxcs/indexPrice.xml"
objHTTP.Open "GET", strURL, False
objHTTP.setRequestHeader "Authorization", "Basic " & Base64Encode(strUserName & ":" & strPassword)
objHTTP.setRequestHeader "Host", "secure.example.com"
objHTTP.setRequestHeader "Content-Type", "text"
objHTTP.send "Nothing"
While objHTTP.readyState <> 4
DoEvents
Wend
Dim strResponseReceived As String
strResponseReceived = objHTTP.responseText
Debug.Print objHTTP.responseText
MsgBox strResponseReceived
Dim xDoc As DOMDocument
Set xDoc = New DOMDocument
xDoc.LoadXML objHTTP.responseText`
''''''''''''''''''''''''''''''''''''''''''
Rest of Code that outputs it into excel
Thanks for the Help!

Convert Html String into HTMLDocument VBA

I'm writing a macro to grab the current exchange rate from yahoo but I'm having trouble converting a html string into a HTMLDocument to allow me to search for the required element by id. Here is my code so far but it fails on the debug.print line.
Public Sub Forex(currency1 As String, currency2 As String)
Dim oXHTTP As Object
Dim doc As HTMLDocument
Dim url As String
Dim html As String
Dim id As String
Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
url = "http://finance.yahoo.com/q?s=" & currency1 & currency2 & "=X"
oXHTTP.Open "GET", url, False
oXHTTP.send
html = oXHTTP.responseText
Set oXHTTP = Nothing
Set doc = New HTMLDocument
doc.body.innerHTML = html
id = "yfs_l10_" & currency1 & currency2
Debug.Print doc.getElementById("id").innerText
End Sub
What am I missing here?
I am making use of method from excel-vba-http-request-download-data-from-yahoo-finance:
Sub Forex(currency1 As String, currency2 As String)
Dim url As String, html As String, id As String
Dim oResult As Variant, oLine As Variant, sRate As String
url = "http://finance.yahoo.com/q?s=" & currency1 & currency2 & "=X"
id = "<span id=""yfs_l10_" & currency1 & currency2 & "=x"">"
html = GetHTTPResult(url)
oResult = Split(html, vbLf)
For Each oLine In oResult
If InStr(1, oLine, id, vbTextCompare) Then
sRate = Split(Split(oLine, "<span id=""yfs_l10_audusd=x"">")(1), "</span>")(0)
Exit For
End If
Next
End Sub
Function GetHTTPResult(sURL As String) As String
Dim XMLHTTP As Variant, sResult As String
Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
XMLHTTP.Open "GET", sURL, False
XMLHTTP.send
sResult = XMLHTTP.responseText
Set XMLHTTP = Nothing
GetHTTPResult = sResult
End Function
You're almost there, you just have the id wrong:
id = "yfs_l10_" & currency1 & currency2 & "=X"
Debug.Print doc.getElementById(id).innerText