Xero Authorization Cancel in ASP.NET

public async Task Callback(string code, string state)
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
                var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
                string ClientID = state.Split(',')[0];
                string CompanyID = state.Split(',')[1];
                string ClientSecret = string.Empty;
                DataTable dt = objIntegration.GetIntegrationCompanyDetail(ClientID, CompanyID);
                if (dt.Rows.Count > 0)
                    XeroConfiguration XeroConfig = new XeroConfiguration
                        ClientId = ClientID,
                        ClientSecret = dt.Rows[0]["ClientSecretKey"].ToString(),
                        CallbackUri = new Uri(ConfigurationManager.AppSettings["XeroCallbackUri"]),
                        Scope = ConfigurationManager.AppSettings["XeroScope"],
                        State = ClientID + "," + CompanyID
                    var client = new XeroClient(XeroConfig, httpClientFactory.CreateClient());
                    var xeroToken = (XeroOAuth2Token)await client.RequestAccessTokenAsync(code);
                    List<Tenant> tenants = await client.GetConnectionsAsync(xeroToken);
                    Tenant firstTenant = tenants[0];
                    string serializedXeroToken = JsonConvert.SerializeObject(xeroToken);
                    objIntegration.UpdateIntegrationDetail(CallbackResonse.Tenants[0].TenantId, CallbackResonse.AccessToken, CallbackResonse.RefreshToken, CallbackResonse.ExpiresAtUtc, serializedXeroToken, ClientID, CompanyID);
            catch (Exception ex)

here this is my code if i am giving cancel in xero authorization it throw No HTTP resource was found that matches the request URI 'https://localhost:/api/Xero/Callback?error=access_denied&state=1B5D7F8C60534E19BC91E3919357B5B1,323'. how to rectify this problem

1 answer

  • answered 2021-04-22 00:00 AlbertGromek

    It looks like there might be an issue with your redirect URI missing the localhost port number. I would suggest checking your redirect URI to make sure it matches the localhost port you are running your project on.

    If the port is correct, you will most likely get a different exception when you try to request an access token using Xero client after a user has cancelled the Authorization:

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

    ( ArgumentException: Parameter is required (Parameter 'code') )

    This is because when the authorization is cancelled, you won't receive a code param in the request, and instead an error param is added to the callback. (error=access_denied)

    You could handle this error by checking for an error param in your callback method.

    var error = HttpContext.Request.Query["error"].ToString();
    if (error == "access_denied")
        //handle the error - redirect back to main/login screen with a message?

    I hope this answer is helpful to you.