XeroAPI / Xero-NetStandard

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

RequestAccessTokenAsync: unknown exception during the call. #333

Open Avarie opened 3 years ago

Avarie commented 3 years ago

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

Describe the bug We are able to get a "code", and all the data are filled correctly.

But, when we try to get an access token, we are getting unknown exception in "RequestAccessTokenAsync".

Support team (Welli) has suggested to create an issue here as this problem directly connected with SDK.

2021-03-17 14:21:23,913 DEBUG #(null) El.Integrations.Xero.XeroClientHelper Received code: ????a9d4db1202f48dddf632917a17d2cace86fbbd93bacf843873b8eb4ce0b6 2021-03-17 14:21:23,913 DEBUG #(null) El.Integrations.Xero.XeroClientHelper use the following xero client configuration : {"AppName":"APP2","ClientId":"????0CE943674030A3375AC58CEED35B","ClientSecret":"????Fmq2sItDGjd-XVmDJRF8qIzHW28FnyZhk31SXCM8RqnK","CallbackUri":"https://????.io/XeroSettings/CallbackMedium","Scope":"offline_access accounting.transactions openid profile email accounting.contacts accounting.settings","State":"/"} 2021-03-17 14:21:23,913 DEBUG #(null) El.Integrations.Xero.XeroClientHelper Try to receive a token. 2021-03-17 14:21:24,116 DEBUG #(null) El.Integrations.Xero.XeroClientHelper Callback is failed System.Exception: An error occurred while sending the request. at Xero.NetStandard.OAuth2.Client.XeroClient.d15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at El.Integrations.Xero.XeroClientHelper.d4.MoveNext() in F:\Jenkins-Workspace_Project\CashDirect\cashdirect-master\El.Integrations.Xero\XeroClientHelper.cs:line 47

The main problem is that we can't understand what is wrong with the api request, as we receive no any explanation.

To Reproduce All the necessary data was posted in CX0010993798 (Xero Tiket ID).

Expected behavior We need to go through the flow without any unknown exceptions.

Additional context I think that you should simplify something to avoid such issues.

jenksguo commented 3 years ago

Hey @Avarie, thanks for reporting the issue and providing the log. Unfortunately we can't derive much information from your internal log. Would you be able to share any more details of the call?

How is your callback controller/endpoint implemented. Share some code snippet so we can help you please.

Avarie commented 3 years ago

Hello, please have a look:

public async Task Callback(string code, XeroTokenApplication application)
        {
            Log.Debug($"Received code: {code} for application ({application})");

            var client = new XeroClient(_settings.GetXeroConfiguration(application));

            Log.Debug($"use the following xero client configuration <json view>:  {FluentJson.Serialize(_settings.GetXeroConfiguration(application))}"); 

            try
            {
                Log.Debug($"Try to receive a token.");
                var responseToken = await client.RequestAccessTokenAsync(code);

                Log.Debug($"Received token: ExpiresAtUtc({responseToken.ExpiresAtUtc:G}),  Tenants: {string.Join(", ", responseToken.Tenants.Select(x => x.ConvertToString()))}");
                var settings = _settings.SetConfiguration(application, responseToken);

                _systemValuesRepository.Save(XeroSettings.XeroServiceSettingsTitle, FluentJson.Serialize(settings));
                Log.Debug($"Everything is ok.");
            }

            catch (Exception ex)
            {
                Log.Debug("Callback is failed", ex);
            }
        }
jenksguo commented 3 years ago

Hey @Avarie thanks for sending the code over. They look fine.

Thanks for emailing the support case number, I was able to see from our logs that you were able to trigger the consent flow successfully on 22 Mar UTC time. So I think the issue had resolved already?

I am closing this for now, please reopen or email that ticket email if you have anything else.

jenksguo commented 3 years ago

Sorry, @Avarie, consent flow doesn't mean the callback code exchange was successful. I will keep looking, it would be good if you can create a new app in developer.xero.com/myapp try or create more test usage on the original app for us.

rlayhbrian commented 2 years ago

Hello @Avarie, apologies for the super-late reply. Has this issue been resolved with the latest version of SDK?

IgnasKav commented 1 year ago

Hi, I had almost the same issue, only the error message was different. For me the problem was with redirectUrl and how c# encodes it.

In xero developer console I added my url like this: https://www.example.com , however it should have been added like this https://www.example.com/ (with a trailing slash). This is because in c# when you do "new Uri("https://www.example.com")" it automatically adds that trailing slash. Then xero tries to compare urls and it fails, throwing error like "unauthorized client" or smth.

P.S. There is a bug in xero dev configuration console, if you try to change the url like https://www.example.com -> https://www.example.com/ it won't work, you need to change it like https://www.example.com -> https://www.example.com/random -> https://www.example.com/