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

RefreshAccessTokenAsync Iussue

Open vikyvardhan opened this issue 4 years ago • 1 comments

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

I'm trying to use the RefreshAccessTokenAsync method in Asp.net MVC C# But the method demands a parameter "XeroToken" and the class of the XeroToken is an interface type in SDK that can't create objects,

So how can create and pass the object in the parameter because of the type is the interface.

Please share if have any method or code to RefreshAccessTokenAsync .

vikyvardhan avatar Oct 11 '21 09:10 vikyvardhan

Not so @vikyvardhan. IXeroToken is an interface, but XeroToken is a class with a simple constructor.

KieranFoot avatar Jul 18 '22 12:07 KieranFoot

Hi vikyvardhan, The SDK provides a XeroOAuth2Token object that implements the IXeroToken interface. This is the object you would pass into the RefreshAccessTokenAsync method. I would recommend looking at our sample app to see how you can refresh tokens. Here is a quick overview:


Our aim is to have a single method to call that will automatically handle retrieving and refreshing a token if expired. We will call this GetXeroToken:

// Retrieve a valid xero token
var xeroToken = await GetXeroToken(XeroConfiguration xeroConfig);

The contents of this method will first retrieve a stored token if exists and check whether it has expired, if it has it will update the stored token value to the refreshed value and return the token:

/// <summary>
/// Get currently stored token, and check whether token has expired, if so, refresh token and return 
/// </summary> 
public async Task<XeroOAuth2Token> GetXeroToken(XeroConfiguration xeroConfig){
	// Get our stored xero token 
	var xeroToken = GetStoredToken();
	
	// Check if token has expired
	if (DateTime.UtcNow > xeroToken.ExpiresAtUtc){
		var client = new XeroClient(xeroConfig);
		xeroToken = (XeroOAuth2Token) await client.RefreshAccessTokenAsync(xeroToken);
         	
		// Update stored token to refreshed token 
		StoreToken(xeroToken);
	}
	return xeroToken;
}

The GetStoredToken helper method simply checks if a file exists: ./xerotoken.json, if it does deserialize the contents and parse it to a XeroOAuth2Token, otherwise, just instantiate a new token

/// <summary>
/// Check if we have a xero token stored, if so, return token. Else return a newly instantiated token
/// </summary> 
public XeroOAuth3Token GetStoredToken(){
	// Check if a token has already been generated and stored in our xerotoken.json file
	if (File.Exists("./xerotoken.json")){
		var tokenString = File.ReadAllText("./xerotoken.json");
		return JsonSerializer.Deserialize<XeroOAuth2Token>(tokenString);
	}
	// If doesn't exist create a new token
	return new XeroOAuth2Token();
}

The StoreToken helper method simply saves the contents of our xero token to a json file called xerotoken.json

/// <summary>
/// Write xero token contents to file
/// </summary> 
public void StoreToken(XeroOAuth2Token xeroToken){
	File.WriteAllText("./xerotoken.json", JsonSerializer.Serialize(xeroToken));
}

Hope this helps :)

JRising-Xero avatar Nov 21 '22 22:11 JRising-Xero