XeroAPI / Xero-NetStandard

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

Parameter is required Parameter name: code #139

Closed tqrecords closed 4 years ago

tqrecords commented 4 years ago

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

Describe the bug Some of our users are getting this error on the Callback method in our app:

public async Task<ActionResult> Callback(string code, string state) { var client = XeroClient(); var iToken = await client.RequestXeroTokenAsync(code);

Parameter is required Parameter name: code

To Reproduce Unsure how to recreate, issue appears to be happening intermittently, goes away when retried.

Expected behavior The 'code' parameter should be available on callback.

jenksguo commented 4 years ago

Hi @tqrecords, I would suggest putting an breakpoint at "var iToken = ... " and inspect the variables.

Using browser developer tools -> network, you can also inspect the redirected call to see if this is missing on the way back form Xero API.

Here is my implementation example:

 public async Task<ActionResult> Callback(string code, string state)
    {
      var client = new XeroClient(XeroConfig.Value, httpClientFactory);

      var xeroToken = (XeroOAuth2Token)await client.RequestXeroTokenAsync(code);

      Console.WriteLine("xeroToken: {0}", xeroToken);

      List<Tenant> tenants = await client.GetConnectionsAsync(xeroToken);

      Tenant firstTenant = tenants[0];

      string serializedXeroToken = JsonSerializer.Serialize(xeroToken);
      System.IO.File.WriteAllText("./xerotoken.json", serializedXeroToken);

      return RedirectToAction("Index", "OrganisationInfo");
    }
jenksguo commented 4 years ago

Hey @tqrecords, I am unable to replicate the issue.

When an oauth flow gets cancelled by a user, it will return the following error query in url. So instead of code & state, it comes back with a error & state.

https://localhost:5001/Authorization/Callback?**error**=access_denied&**state**=my_state#_=_

I can write a new method in the sample app to demonstrate how this can be handled.

Marking this as an improvement suggestion.

jenksguo commented 4 years ago

Closing this issue due to inactivity. @tqrecords please re-open if you need.

Anmol16q commented 2 years ago

I've tried it says forbidden Pls check try {

            //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            var xeroToken = (XeroOAuth2Token)await _XeroClient.RequestXeroTokenAsync(code);

            List<Tenant> tenants = await _XeroClient.GetConnectionsAsync(xeroToken);
            Tenant firstTenant = tenants[0];
            TokenUtilities.StoreToken(xeroToken);
        }
        catch (Exception ex)
        {
        }