我正在尝试使用 capital.com api 使用 python 学习算法交易。但是,许多调用需要 CST 令牌和 X-SECURITY-TOKEN。在文档中,它说在启动会话时应该在响应标头中找到它们,但这并没有发生。
文档:https://open-api.capital.com
我已经尝试使用 CST 令牌的 clientId 和 X-SECURITY-TOKEN 的 accountId,但这不起作用。到目前为止,这是我的代码:
import http.client
import json
import api
conn = http.client.HTTPSConnection("demo-api-capital.backend-capital.com")
payload = json.dumps({
"identifier": api.EMAIL,
"password": api.PASSWORD,
})
headers = {
'X-CAP-API-KEY': api.KEY,
'Content-Type': 'application/json'
}
conn.request("POST", "/api/v1/session", payload, headers)
res = conn.getresponse()
data = json.loads(res.read().decode("utf-8"))
print(data)
cst = data['clientId']
secuirity_token = data['currentAccountId']
accounts_payload = ''
accounts_headers = {
'X-SECURITY-TOKEN': secuirity_token,
'CST': cst
}
conn.request("GET", "/api/v1/accounts", accounts_payload, accounts_headers)
accounts_res = conn.getresponse()
accounts_data = accounts_res.read()
print(accounts_data.decode("utf-8"))