excelvbaapiwinhttprequest

Session ID not refreshing in API call


I am trying to use VBA Excel to access the API provided by the website www.myfxbook.com. The API documentation is here(https://www.myfxbook.com/fr/api). The Steps to get the data are as follows:

  1. Login through Login API
  2. Get session ID of the session from response of Login API
  3. Get data based on this Session ID through various other APIs
  4. Logout through the Logout API to generate a new session

The Problem I am facing is that even though the Login and Logout APIs are being used and throwing no error, I am always getting the same Session ID when I am trying to use it through Excel VBA. On the other hand, using the same URLs through Python or even the browser is giving me a different session ID each time. i can only use Excel for this project. Could someone please help me how to get different session ID on successful logout?

I am using the code below to do this. I have hidden (*******) the Email ID and password for security purposes.

Sub extract()

Dim sht1 As Worksheet
Dim email As String
Dim password As String
Dim accountName As String
Dim url As String
Dim hreq As Object
Set hreq = CreateObject("MSXML2.XMLHTTP")
Dim accountID As String

Set sht1 = Sheets(1)
email = "*******"
password = "********"
accountName = "all day multiple currency coinexx"
loginURL = "https://www.myfxbook.com/api/login.xml?email=" + email + "&password=" + password
hreq.Open "GET", loginURL, False
hreq.Send
Dim xmlDoc As New MSXML2.DOMDocument60
Dim response As String
response = hreq.ResponseText
If Not xmlDoc.LoadXML(response) Then
    MsgBox ("Load Error")
End If

Dim xnodelist As MSXML2.IXMLDOMNodeList
Set xnodelist = xmlDoc.getElementsByTagName("session")
Dim sessionID As String
sessionID = xnodelist(0).Text

'Do Something here to get data. This part is working fine.

logoutURL = "https://www.myfxbook.com/api/logout.xml?session=" + sessionID
hreq.Open "GET", logoutURL, False
hreq.Send
response = hreq.ResponseText
If Not xmlDoc.LoadXML(response) Then
    MsgBox ("Load Error")
End If

End Sub

Solution

  • I think it likely you are hitting caching. Try to force an avoidance of this with an additional header

    hreq.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"