会话 ID 在 API 调用中未刷新 [英] Session ID not refreshing in API call

查看:20
本文介绍了会话 ID 在 API 调用中未刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 通过登录 API 登录
  2. 从登录 API 的响应中获取会话的会话 ID
  3. 通过各种其他 API 获取基于此会话 ID 的数据
  4. 通过 Logout API 注销以生成新会话

我面临的问题是,即使正在使用登录和注销 API 并且没有抛出任何错误,当我尝试通过 Excel VBA 使用它时,我总是得到相同的会话 ID.另一方面,通过 Python 甚至浏览器使用相同的 URL 每次都会给我一个不同的会话 ID.我只能在这个项目中使用 Excel.有人可以帮助我如何在成功注销时获取不同的会话 ID?

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?

我正在使用下面的代码来做到这一点.出于安全考虑,我已隐藏 (*******) 电子邮件 ID 和密码.

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

推荐答案

我认为您可能遇到了缓存问题.尝试使用附加标题强制避免这种情况

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"

这篇关于会话 ID 在 API 调用中未刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆