【问题标题】:Session ID not refreshing in API call会话 ID 在 API 调用中未刷新
【发布时间】:2021-09-03 00:44:53
【问题描述】:

我正在尝试使用 VBA Excel 访问网站www.myfxbook.com 提供的 API。 API 文档在这里(https://www.myfxbook.com/fr/api)。获取数据的步骤如下:

  1. 通过登录 API 登录
  2. 从登录 API 的响应中获取会话的会话 ID
  3. 通过各种其他 API 根据此 Session ID 获取数据
  4. 通过 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"
  • 哇,它成功了! Excel Vba 继续让我惊讶。您可以将其写为答案,以便我可以选择它。非常感谢:)
  • 不用担心。完成。

标签: excel vba api winhttprequest


【解决方案1】:

我认为您可能正在使用缓存。尝试使用额外的标题来避免这种情况

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2019-04-28
    • 2011-10-29
    相关资源
    最近更新 更多