问题
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 = 571738634.47873.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);
}
}
}
回答1:
It was missing User-Agent from the header. client.DefaultRequestHeaders.Add("User-Agent", "Console");
来源:https://stackoverflow.com/questions/64597071/making-post-request-from-console-app-results-in-error-403-access-denied