403对Interactive Brokers客户端门户网站API的任何POST请求的响应代码



我尝试使用Interactive Brokers客户端门户网站API:

  • 我成功启动网关并登录
  • 我成功地向各个端点发出了GET请求
  • 当我向任何端点发出POST请求时,我会收到403响应代码

例如,我尝试更新当前选择的帐户:

// it is need for change User-Agent as recommended*
chrome.webRequest.onBeforeSendHeaders.addListener(function (details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'User-Agent') {
details.requestHeaders[i].value = 'Console';
break;
}
}
return {requestHeaders: details.requestHeaders};
}, {urls: ['<all_urls>']}, ['blocking', 'requestHeaders']);
// it is code of make of POST request
var data = {};
data['acctId'] = 'U1234567';
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://localhost:5000/v1/portal/iserver/account', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
sendResponse({Status: xhr.status + '_' + xhr.responseText}); // I have "403_" response
}
};

*:从控制台应用程序发出POST请求导致错误403-拒绝访问

我请求的标题:

Cookie: SBID=xskr4cf7kflki7wrzzy; XYZAB_AM.LOGIN=29c2d567b3a3f23d8d02bdd5ef78d0c8c2694438; XYZAB=29c2d567b3a3f22d8d12bdd5ef78d0c8c2694438; api=673d21e86cf499f04b985446a90a844b; ibkr.nj=286565962.20480.0000
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,uk;q=0.6
Accept-Encoding: gzip, deflate, br
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: none
Origin: chrome-extension://jnblchdcckkmedocgchlfmfhbaihjdnl
Accept: */*
Content-Type: application/json
User-Agent: Console
Content-Length: 21
Connection: close
Host: localhost:5000

我的请求正文:

{"acctId":"U1234567"}

我通过填充用户代理并发送一个空字符串作为POST负载来解决这个问题,以确保设置了Content-Length标头,例如对/tlickle端点的调用。

如上所述,我们可以通过在标头中添加"用户代理":"控制台"来解决此问题。您可以使用Python请求发送标题,如下所示。

import requests
headers = {'User-Agent': 'Console', 'content-type': 'application/json'}
response = requests.post(url, data=data, headers=headers, verify='path to .pem file or False if SSL is not needed')

其中url是包含postURL的字符串,data是需要提交给post请求的json数据。

我解决了这个问题。事实上,"客户端门户WebAPI网关"不仅仅是一个网关,它还是一个服务器,允许您承载带有代码的HTML页面。按照此处所述启动网关之后https://interactivebrokers.github.io/cpwebapi/index.html#login和授权https://localhost:5000/页你可以去https://localhost:5000/demo/#/并看到一个带有非常复杂代码的小型演示应用程序,这超出了我的原始知识范围。因此,我用自己的文件替换了root\webapps\demo文件夹中的index.html文件,并为POST请求编写了自己的javascript代码

var data={};
data["symbol"]="RSG";
data["name"]=false;
data["secType"]="STK";
var xhr=new XMLHttpRequest();
xhr.open("POST", "https://localhost:5000/v1/portal/iserver/secdef/search", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(data));
xhr.onreadystatechange=function()
{
if (xhr.readyState==4)
{
console.log(xhr.status);
console.log(xhr.responseText);
}
}

它成功了,因为现在我的应用程序的index.html页面和端点在同一个域localhost:5000上,所以mod_security拒绝不会发生,一切都正常!

您是否执行了不安全的https POST请求?

就我而言,不安全的https GET请求运行良好,但我认为它会生成";403〃;用于POST请求。在我尝试使用自签名证书的路上

问题被标记为已解决,但我只需将标头中的User Agent设置为任何值,就解决了类似的问题。我当前的应用程序是使用请求模块的NodeJS。将"用户代理"设置为任意值,并确保处理不安全的SSL证书。