Xero-NetStandard icon indicating copy to clipboard operation
Xero-NetStandard copied to clipboard

OAuth2 Client RequestClientCredentialsTokenAsync Error

Open mattpawson opened this issue 4 years ago • 8 comments

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

  • OAuth2 v3.20.0
  • OAuth2Client v1.5.1

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.

mattpawson avatar Oct 20 '21 16:10 mattpawson

@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?

RettBehrens avatar Nov 03 '21 15:11 RettBehrens

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.

mattpawson avatar Nov 03 '21 16:11 mattpawson

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?

RettBehrens avatar Nov 03 '21 18:11 RettBehrens

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.<RequestClientCredentialsTokenAsync>d__15.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.<CreateApi>d__4.MoveNext() in {PATH}\V2\Xero V2\XeroServices.cs:line 39"

mattpawson avatar Nov 04 '21 10:11 mattpawson

@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.

ckapatch avatar Nov 05 '21 18:11 ckapatch

@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!

mattpawson avatar Nov 05 '21 18:11 mattpawson

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.

ckapatch avatar Nov 05 '21 18:11 ckapatch

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...

mattpawson avatar Nov 05 '21 18:11 mattpawson