我正在尝试使用VBA Excel访问网站提供的APIwww.myfxbook.com。 API文档在这里(https://www.myfxbook.com/fr/api)。获取数据的步骤如下:
- 通过登录API登录
- 从登录 API 的响应中获取会话的会话 ID
- 通过各种其他 API 根据此 Session ID 获取数据
- 通过 Logout API 注销以生成新会话
我面临的问题是,即使正在使用登录和注销 API 并且没有抛出任何错误,但当我尝试通过 Excel VBA 使用它时,我总是得到相同的会话 ID。另一方面,通过 Python 甚至浏览器使用相同的 URL 每次都会给我不同的会话 ID。我只能使用 Excel 来完成这个项目。有人可以帮我如何在成功注销时获取不同的会话 ID 吗?
我正在使用下面的代码来执行此操作。出于安全目的,我隐藏了 (********) 电子邮件 ID 和密码。
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
我认为您可能正在使用缓存。尝试使用附加标头来强制避免这种情况
hreq.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)