XeroAPI / Xero-NetStandard

A wrapper of the Xero API in the .NetStandard 2.0 framework. Supports Accounting, Payroll AU/US, and Files
MIT License
118 stars 119 forks source link

OAuth2 Client RequestClientCredentialsTokenAsync Error #397

Open mattpawson opened 2 years ago

mattpawson commented 2 years ago

SDK you're using (please complete the following information):

Describe the bug Unable to use the call "token = await client.RequestClientCredentialsTokenAsync();". It results in "An error occurred while sending the request." This code works fine, without issues, when called from a console application, but when attempted to be used within a WebApi2 application the issue occurs.

To Reproduce

try
{
    XeroConfiguration xeroConfiguration = new XeroConfiguration
    {
        ClientId = [CLIENT_ID],
        ClientSecret = [CLIENT_SECRET]
    };

    client = new XeroClient(xeroConfiguration);
    token = await client.RequestClientCredentialsTokenAsync();
}
catch (Exception e)
{
    throw new Exception("An error occurred", e);
}

Expected behavior Expect to receive token to allow continuation of processing requests.

RettBehrens commented 2 years ago

@mattpawson just looking into WebApi2 and see that it can be run server-side or client-side. Can you please confirm which you are doing for your use case?

mattpawson commented 2 years ago

Hi @RettBehrens. I'm running it as a server side application. Basically, in this instance, the client facing web portal connects to a WebAPI2 API that then utilises the SDK to pull the account codes and which it then passes back to the client.

RettBehrens commented 2 years ago

Hmm, my best guess was that it was an issue running from client-side but since we've ruled that out, do you have visibility into the inner exception messages? Can you post the stack trace?

mattpawson commented 2 years ago

There doesn't appear to be an inner exception: Message: "An error occurred while sending the request." Source: "Xero.NetStandard.OAuth2Client" Stacktrace: " at Xero.NetStandard.OAuth2.Client.XeroClient.d15.MoveNext()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at ClickinOn.{REMOVED}.XeroClassLibrary.v2.XeroServices.d4.MoveNext() in {PATH}\V2\Xero V2\XeroServices.cs:line 39"

ckapatch commented 2 years ago

@mattpawson if you're using an older .NET 4.x project you'll need to ensure that your requests are being sent using TLS 1.2.

Add this line to your startup configuration:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I had this exact issue and after digging into it found this was the culprit.

mattpawson commented 2 years ago

@mattpawson if you're using an older .NET 4.x project you'll need to ensure that your requests are being sent using TLS 1.2.

Add this line to your startup configuration:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I had this exact issue and after digging into it found this was the culprit.

What a star you are!!

That was the exact same problem! How did you manage to find out what the problem was just from the exception / stacktrace?

Cheers!

ckapatch commented 2 years ago

Pulled down the Xero library to debug it locally, found it was using IdentityModel to execute the requests for oauth2. So pulled that library and ran them all locally to determine that the underlying exception was "The request was aborted: Could not create SSL/TLS secure channel" which lead me to the TLS 1.2 solution.

mattpawson commented 2 years ago

You're a better man than I then! Thanks for your help!

Begs the question why this exception wasn't bubbled up to OAuth2Client, even if in an inner exception though...