admin管理员组

文章数量:1316030

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.

本文标签: cTrouble Connecting to IBKR Web API V10 (403 Error)Stack Overflow