Hi,
I’m developing a C# server-side application to integrate with the FreeAgent API.
My goal is to programmatically refresh an OAuth2 token by making a POST request to the /v2/token_endpoint. This was working for over a year and earlier this week it suddenly stopped working. This was my code I used previously:
static void Authenticate()
{
try
{
var client = new RestClient(base_url);
var request = new RestRequest("v2/token_endpoint", Method.Post);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "refresh_token");
request.AddParameter("client_id", api_key);
request.AddParameter("client_secret", api_secret);
request.AddParameter("refresh_token", refresh_token);
var response = client.Execute(request);
if (response.IsSuccessful)
{
FreeAgentAuthResponse free_response = JsonConvert.DeserializeObject<FreeAgentAuthResponse>(response.Content);
access_token = free_response.access_token;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
However, all my requests are being blocked by a Cloudflare JavaScript challenge. Because my application is a backend service and not a browser, it cannot solve this challenge, so I never reach the API endpoint anymore.
I have already tried the standard workarounds, such as setting browser-like User-Agent and Accept headers, but the block persists. Using a browser automation tool like Selenium is not a stable solution for a production environment. Something has obviously changed that I need to address in my code, possibly around User-Agent headers etc??
Could you please advise on the correct and officially supported method for a server-side application to communicate with the API without being blocked by the Cloudflare firewall?
Thanks for your help.