最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Trouble Connecting to IBKR Web API V1.0 (403 Error) - Stack Overflow

programmeradmin1浏览0评论

Has anyone successfully connected to the IBKR Web API V1.0 using C#? Any working sample or insights into why this might be happening would be greatly appreciated. (PS: I am not asking about TWS or IBGateWay)

I have successfully connected to the IBKR Web API V1.0 Gateway using Postman, following the official documentation. As you see in the following picture, no authorization or additional headers were required in Postman:

However, when I attempt to make the same request in a C# console application, I receive the following error: Error 403 - Access Denied.

I couldn't find a single working C# example for this API, most of the available samples are in Python. I tried mimicking the Python code (for example) in C#, but I still get the same error.

This is my simple Console application for the same Postman call:

static async Task Main()
{
    HttpClientHandler handler = new HttpClientHandler();
    handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
    HttpClient client = new HttpClient(handler);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = await client.GetAsync("https://localhost:5000/v1/api/iserver/auth/status");
    string res = await response.Content.ReadAsStringAsync();
    Console.WriteLine(res);
}

Has anyone successfully connected to the IBKR Web API V1.0 using C#? Any working sample or insights into why this might be happening would be greatly appreciated. (PS: I am not asking about TWS or IBGateWay)

I have successfully connected to the IBKR Web API V1.0 Gateway using Postman, following the official documentation. As you see in the following picture, no authorization or additional headers were required in Postman:

However, when I attempt to make the same request in a C# console application, I receive the following error: Error 403 - Access Denied.

I couldn't find a single working C# example for this API, most of the available samples are in Python. I tried mimicking the Python code (for example) in C#, but I still get the same error.

This is my simple Console application for the same Postman call:

static async Task Main()
{
    HttpClientHandler handler = new HttpClientHandler();
    handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
    HttpClient client = new HttpClient(handler);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = await client.GetAsync("https://localhost:5000/v1/api/iserver/auth/status");
    string res = await response.Content.ReadAsStringAsync();
    Console.WriteLine(res);
}
Share Improve this question edited Jan 30 at 7:28 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Jan 30 at 4:59 Sharif YazdianSharif Yazdian 4,7484 gold badges23 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Found that I have to add this header to the C# API call: request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0 Safari/537.36");

So the working code is:

   static async Task Main()
  {
      HttpClientHandler handler = new HttpClientHandler();
      handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
      HttpClient client = new HttpClient(handler);
      client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0 Safari/537.36");
      HttpResponseMessage response = await client.GetAsync("https://localhost:5000/v1/api/iserver/auth/status");
      string res = await response.Content.ReadAsStringAsync();
      Console.WriteLine(res);
  }

There is no documentation for Ibrk C# Api.

发布评论

评论列表(0)

  1. 暂无评论