1

How to handle Xero Callback error using asp.net

2 Answers2

3

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.

AlbertGromek
  • 460
  • 3
  • 6
0

Have you read through the README for the Xero-NetStandard repo?

First you need to send the user through the authorize URL

return Redirect(client.BuildLoginUri());

Then when they are returned, you exchange the temporary code for a valid token set which will contain an access_token and refresh_token.

https://github.com/XeroAPI/Xero-NetStandard

SerKnight
  • 2,502
  • 1
  • 16
  • 18