Search code examples
htmlvbaoauthaccess-token

VBA Oauth parse html responsetext


I am getting http response as HTML and I am trying to fetch the token from the response. The response looks like the code below:

<title>Token</title>
<style>
input { white-space: nowrap; width: 500px; overflow: hidden; text-overflow: ellipsis; }
</style>
<div>
<input id="theToken" type="text" value="Bearer oaxgggfh12tpjhudhsbjj">
<button class="btn" onclick="copyToken()">Copy</button>
</div>
<div>
<label>(expires: 22.07.22, 21:18)</label>
</div>
<script>
function copyToken() { document.getElementById("theToken").select(); document.execCommand("copy"); }
</script>

Here I need the value of id "theToken". So far I managed to access div using

Set topics = html.getElementsByTagName("div")

But I cannot fetch the actual token string.

My oauth token request function looks like below:

    Dim html As New HTMLDocument
    Set XMLHTTP_swlc = CreateObject("MSXML2.XMLHTTP")
    
    strurl = "https:***remoting/oauth-token?login=true"
    With XMLHTTP_swlc
    .Open "POST", strurl, False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Accept", "application/json"
        .setRequestHeader "Authorization", "Bearer" & "rlZ....PT0"
        .send
        
        ivari2 = .status
        Debug.Print ivari2
        
        
        html.Body.innerHTML = XMLHTTP_swlc.responseText
        Set tokn = html.getElementById("theToken").value

Please help. Thanks in advance!


Solution

  • If I copy and paste your provided HTML into a worksheet cell, and then run this:

    Dim html As New HTMLDocument
    html.body.innerHTML = [A1].Value   'your provided HTML fragment
    Debug.Print html.getElementById("theToken").Value 
    

    ...I get output Bearer oaxgggfh12tpjhudhsbjj as expected. If you're not seeing that then maybe your responseText is not what you expect.