5

I am trying to use the new IBKR Client Portal API. I have written a simple console program to access the API but it results in Error 403 - Access denied. If I try the same request from Postman it seems to be working. I tried using fiddler to see the request that console app sends but that results in a separate error. How can I diagnose this issue?

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var baseURL = "https://localhost:5000/v1/portal";
            HttpClientHandler handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
            var client = new HttpClient(handler);
            client.DefaultRequestHeaders.Add("Cookie", "ibkr.il = 123456789.123456.0000");
            client.DefaultRequestHeaders.Add("AcceptEncoding", "gzip, deflate, br");
            var response = await client.GetAsync(baseURL + "/sso/validate");
            string result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
        }
    }
}
null
  • 713
  • 2
  • 8
  • 30
  • 3
    You try to implement the `ServerCertificateCustomValidationCallback` yourself, so you can put a breakpoint there to see if it hits. Other then that, compare headers from Postman and code. – Michael Oct 29 '20 at 20:29

1 Answers1

5

It was missing User-Agent from the header. client.DefaultRequestHeaders.Add("User-Agent", "Console");

null
  • 713
  • 2
  • 8
  • 30
  • 1
    Thanks a lot! You have saved me many hours of searching for the solution – core Dec 27 '20 at 21:31
  • 1
    Man... I have spent 2 days wondering why is it 403, it didn't make any sense. You saved my life and I'm quite upset about their poor documentation, this header should be part of the prerequisites. Again, thank you, now I can proceed with my algorithm. – Youssef Mar 18 '21 at 00:04