Skip to content

Instantly share code, notes, and snippets.

@wadewegner
Last active August 29, 2015 13:56
Show Gist options
  • Save wadewegner/8834571 to your computer and use it in GitHub Desktop.
Save wadewegner/8834571 to your computer and use it in GitHub Desktop.
private async void btnExpireToken_Click(object sender, RoutedEventArgs e)
{
_token.AccessToken = "GARBAGE";
var response = await RetryMethod<dynamic>(GetAccounts, 3, 0, RefreshToken);
}
private async Task RefreshToken()
{
var auth = new AuthenticationClient();
await auth.TokenRefreshAsync(ConsumerKey, _token.RefreshToken);
_token.AccessToken = auth.AccessToken;
}
private async Task<dynamic> GetAccounts()
{
var client = new ForceClient(_token.InstanceUrl, _token.AccessToken, "v29.0");
var accounts = await client.QueryAsync<dynamic>("SELECT id, name, description FROM Account");
return accounts;
}
public async Task<T> RetryMethod<T>(Func<Task<T>> method, int numRetries, int retryTimeout, Func<Task> onInvalidTokenAction)
{
var refreshToken = false;
T retval = default(T);
do
{
if (refreshToken)
{
await onInvalidTokenAction();
refreshToken = false;
}
try
{
retval = await method();
return retval;
}
catch (ForceException ex)
{
if (ex.Message == "Session expired or invalid")
{
refreshToken = true;
}
}
} while (numRetries-- > 0);
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment